[SQL ] 请教一个插入语法[已解决]

楼主: pili100 (zelda)   2017-06-23 17:37:15
(针对 SQL 语言的问题,用这个标题。请用 Ctrl+Y 砍掉这行)
数据库名称:mariadb
数据库版本:10.1
内容/问题描述:
请教版上大神
我有两个表,例
tableA 有两字段:colA,colA2
tableB 有一字段:colB,key
tableB 的key是colA的外键
我想达成的目标是
如果colB某值不存在则插入资料,且key是需要找colA2的值来对应填入
根据我找网络上的文件
写了这么一段
insert into tableB(colB, key)
select 'c0-123', tableA.colA from tableB
join tableA on tableB.key=tableA.colA where tableA2='c0'
where not exists( select colB from tableB where colB='c0-123');
不过到后面提示where not exists这段是错误的
不晓得如何修改才能达到我想要的结果
还请高手指点
作者: vanchy (vanchy)   2017-06-24 10:57:00
我猜:用exist时,巢状SQL里的table要跟主SQL的table建立relation。
作者: flowwinds (..)   2017-06-24 16:27:00
改为..FROM tableA WHERE tableA2='c0' AND NOT EXISTS.. 试试看

Links booklink

Contact Us: admin [ a t ] ucptt.com