加入MySQL的类型我有2个表结构如下动态设置通过PHP
产品
[id] [name] [setting_id]
1 pendrive 6
2 laptop 3
3 mobile 2
4 vallet 7
5 clothes 4
store_product
[id] [name] [storeid]
2 laptop_store 1
3 mobile_mobile 1
5 clothes_store 4
8 new1_store 2
9 new2_store 3
我想造成这样的
[id] [name]
1 pendrive
2 laptop_store
3 mobile_mobile
4 vallet
5 clothes_store
8 new1_store
9 new2_store
我曾尝试此查询,但只给出了但没有new1_store和new1_store
SELECT products.id , CASE WHEN store_product.name IS NOT NULL AND store_product.name!='' THEN store_product.name ELSE products.name END AS name
from products
left join store_product on store_product.id = products.id
当我从左边知道加入它是不可能的,但可以请提出优化的查询(避免工会的左连接和右连接)。如果可能比请回答
感谢
很好的答案...但它可能没有工会?.. thnks – 2014-10-08 09:42:19
你不想要一个联盟,你不想要的左,右外连接既不是一个组合。我很抱歉地说,但这是你的设计是错误的。对于各种产品,您应该只有一个表格,并且有适当的字段来区分它们的类型。 – SylvainL 2014-10-08 09:55:37