我很难做到这一点。
我有一个名为 1_related 的表
id,prod_pack,fk_prod_pack_id,fk_prod_pack_related,fk_prod_pack_related_id
'2', '0', '102', '0', '2482'
'3', '0', '102', '0', '3147'
'2294', '0', '102', '1', '10'
我还有另外 2 个表,分别称为 1_products 和 1_packages
我想根据 fk_prod_pack_related 的值加入任一表的内容(0 表示 1_products 表,1 表示 1_packages 表)
此查询从 1_related 返回第 2 行和第 3 行,并连接 1_products 的内容
SELECT
r . *, p . *
FROM
1_related AS r
INNER JOIN
1_products as p ON r.fk_prod_pack_related_id = p.id
and r.fk_prod_pack_related = 0
WHERE
r.prod_pack = 0
and r.fk_prod_pack_id = 102
但我也需要来自 1_packages 的数据,所以我尝试了这个,但它什么也没返回
SELECT
r . *, p . *, s . *
FROM
1_related AS r
INNER JOIN
1_products as p ON r.fk_prod_pack_related_id = p.id
and r.fk_prod_pack_related = 0
INNER JOIN
1_packages as s ON r.fk_prod_pack_related_id = s.id
and r.fk_prod_pack_related = 1
WHERE
r.prod_pack = 0
and r.fk_prod_pack_id = 102