获取表

时间:2016-12-18 15:21:39

标签: mysql

我有两张桌子

product_categories
------------------
id PK AUTO
category

store_product_categories
------------------------
id PK AUTO
store_id FK
category FK (product_categories id)

现在我想要获取不在store_product_categories

中的行

这是我设计的查询,但没有给我结果

 SELECT * FROM store_product_categories spc 
     LEFT JOIN product_categories pc
     on spc.category=pc.id 
     WHERE spc.store_id=23 AND pc.id IS NULL

1 个答案:

答案 0 :(得分:2)

您无法从store_product_categories 中选择获取不在该表中的行。所以,我推测你想要不在表格中的类别

SELECT pc.*
FROM product_categories pc LEFT JOIN
     store_product_categories spc 
     ON spc.category = pc.id AND spc.store_id = 23 
WHERE spc.category IS NULL;

请注意,spc_store_id = 23位于ON子句中,因为条件位于第二个表中。

相关问题