我有两个表,attributes 和 attributes_entries
attributes 保存标题,attribute_entries 保存该标题的值。
如果我做:
SELECT attribute_entries.title AS ent_title,
attributes.title AS attr_title
FROM attribute_entries
INNER JOIN attributes
ON attribute_entries.FK_attribute_id = attributes.id
WHERE attribute_entries.title = 'ford'
AND attributes.title = 'manufacturer'
我可以得到以下结果:
ent_title | attr_title
ford | manufacturer
如果我做另一个查询:
SELECT attribute_entries.title AS ent_title,
attributes.title AS attr_title
FROM attribute_entries
INNER JOIN attributes
ON attribute_entries.FK_attribute_id = attributes.id
WHERE attribute_entries.title = 'whatev'
AND attributes.title = 'nickname'
我得到以下结果:
ent_title | attr_title
whatev | nickname
whatev | nickname
这两个查询都与我在那里的数据一致。
所以我认为这会起作用:
SELECT attribute_entries.title AS ent_title,
attributes.title AS attr_title
FROM attribute_entries
INNER JOIN attributes
ON attribute_entries.FK_attribute_id = attributes.id
WHERE attribute_entries.title = 'ford'
AND attributes.title = 'manufacturer'
AND attribute_entries.title = 'whatev'
AND attributes.title = 'nickname'
这显然是行不通的。在一份声明中获得结果的最佳方法是什么?
您应该使用对过滤:
或者你可以使用 IN
如果您需要两个条件都为真,则可以使用 EXISTS:
你可以检查它:db<>fiddle here