简体   繁体   English

MySQL-LEFT JOIN无法正常工作

[英]MySQL - LEFT JOIN not working as expected

I have 2 tables. 我有2张桌子。

attribute
=========
ID  Name
-------------------
1   Size
2   Colour


product_attribute
=========
ID  attribute_id
-------------------
1   1

I want to show any values contained in the attribute table that are missing from the product_attribute table. 我想显示product_attribute表中缺少的属性表中包含的任何值。

This code results in "size" when I expected "colour". 当我期望“颜色”时,此代码将导致“大小”。 What am I missing? 我想念什么?

SELECT name FROM attribute a LEFT JOIN product_attribute p ON a.id = p.attribute_id
SELECT a.name FROM attribute a 
LEFT JOIN product_attribute p ON a.id = p.attribute_id
where p.attribute_id is null

您可以改用:

SELECT name FROM attribute where ID  not in (select attribute_id from product_attribute)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM