我在尝试将某些值插入表格时遇到问题。我用字段
创建了一个空表id(primary key)
association_id
resource_id
我有另一张表
resource_id
association_id
和另一个
id(coresponding to the association_id in the former one)
image
我想从第一个填充的表中插入resource_id
和association_id
,其中最后一个表中相应ID的图像字段不为空。
我试过了:
INSERT IGNORE INTO `logo_associations` (``,`association_id`,`resource_id`)
SELECT
``,
`a`.`association_id`,
`a`.`resource_id`
FROM doc24_associations_have_resources a
Join doc24_associations An on a.association_id = An.id
WHERE An.image<>''
但它不起作用
答案 0 :(得分:1)
试试这个:
INSERT INTO logo_associations (association_id, resource_id)
SELECT a.association_id
,a.resource_id
FROM doc24_associations_have_resources a
LEFT JOIN doc24_associations an ON a.association_id = an.id
WHERE an.image IS NULL -- check for null with left join
这对SQL Server有效。您不需要选择并插入第一列,因为它是您提到的标识。
答案 1 :(得分:0)
我的经验基于SQL Server,但SQL可能非常相似
INSERT INTO DestinationTable
(association_id, resource_id)
SELECT LNK.assocication_id,
LNK.resource_id
FROM LinkTable AS LNK
INNER JOIN ImageTable AS IMG ON IMG.id = LNK.association_id
AND IMG.image IS NOT NULL
上面我假设以下内容: