编辑2:
这里没有什么可以找到的,它是我在其他地方的失败。
谁知道!= NULL
在MySQL中不起作用。
版主删除此内容?MySQL加入问题
我得到了一个eav
数据库,我需要从中选择一些具有属性的产品。
当我使用
select attribute1.value as a1, attribute2.value as a2, products.id
from attributes attribute1, attributes attribute2, products
where product.id = attribute1.product_id and attribute1.name = 'abc' and
product.id = attribute2.product_id and attribute2.name = 'def'
当一个属性上缺少一个产品我没有得到所有的产品,我需要得到所有产品NULL
如果有缺少的属性。
当我使用
select attribute1.value as a1, attribute2.value as a2, products.id
from products
left join attributes as attribute1 on (product.id = attribute1.product_id and attribute1.name = 'abc')
left join attributes as attribute2 on (product.id = attribute2.product_id and attribute2.name = 'def')
我得到的所有产品,但所有产品都a1 = NULL
即使他们不数据库时。
问题是什么?
实施例:
产品:
id
1000
1001
1002
1003
属性:
name product_id value
abc 1000 1
abc 1002 2
def 1000 3
def 1001 4
预期结果:从第一查询
id a1 a2
1000 1 3
1001 NULL 4
1002 2 NULL
1003 NULL NULL
结果:
0从第二查询id a1 a2
1000 1 3
结果:
id a1 a2
1000 NULL 3
1001 NULL 4
1002 NULL NULL
1003 NULL NULL
编辑:
固定第二个查询和示例。
可否请你发布一些示例数据库条目,你会从查询什么期望?你的第二个查询似乎也缺少了FROM子句。 – Zulan 2011-02-25 21:49:59
@Zulan添加编辑 – Dani 2011-02-25 21:57:50
除了一些更多的拼写错误(product ** s ** .id),编辑后的查询是正确的。它应该给出正确的结果。 – Zulan 2011-02-25 22:08:10