MySQL 多态关联连接的 ID returns 错误
MySQL Wrong id returns for Polymorphic Association Joins
我与两个 table 有多态关联。 (我知道它很乱,但我暂时不得不离开)
在向主 table 查询时,我需要加入两个 table 的结果。我接近我想要的结果,但在我的情况下 master table id returns 是错误的。
数据结构
Table A
|-- id
|-- type
|-- type_id
|-- property_a1
|-- property_a2
Table B
|-- id
|-- property_b1
|-- property_b2
Table C
|-- id
|-- property_c1
|-- property_c2
预期输出
我想要下面的结果。每行包含所有三个 table 的所有字段(或通过查询选择)。如果给定的关联 table 中缺少 属性,则分配 Null 或空字符串。
Result Row
|-- id
|-- property_a1
|-- property_a2
|-- type
|-- type_id
|-- property_b1 // if property from Table c value = ''
|-- property_b2 // if property from Table c value = ''
|-- property_c1 // if property from Table b value = ''
|-- property_c2 // if property from Table b value = ''
我目前的工作可以在这个sqlfiddle:
中看到
http://sqlfiddle.com/#!9/2d05e/17
SQL 我使用的查询
来自fiddle。
SELECT
product.id,
res.name,
res.fruit_id,
res.vegetable_id,
res.is_green,
res.color,
product.price,
res.seasonal
FROM
product
JOIN
(
SELECT
fruit.id AS fruit_id,
0 AS vegetable_id,
fruit.name,
fruit.color,
fruit.seasonal,
false as is_green
FROM
fruit
UNION
SELECT
0 as fruit_id,
vegetable.id AS vegetable_id,
vegetable.name,
'' as `color`,
false as `seasonal`,
vegetable.is_green
FROM
vegetable
) res ON product.type_id = res.fruit_id OR product.type_id = res.vegetable_id
我可以使用 GROUP BY
但这只会删除必要的数据。
试试这个
SELECT product.id, res.name, res.fruit_id, res.vegetable_id,
res.is_green,res.color,product.price,res.seasonal
FROM product
JOIN
(
SELECT fruit.id AS fruit_id,
0 AS vegetable_id,fruit.name,fruit.color,fruit.seasonal,
false as is_green
FROM fruit
UNION
SELECT 0 as fruit_id,vegetable.id AS vegetable_id,
vegetable.name,'' as `color`,false as `seasonal`, vegetable.is_green
FROMvegetable
) res ON (product.type_id = res.fruit_id AND product.product_type = 'fruit')
OR (product.type_id = res.vegetable_id AND product.product_type = 'vegetable')
我与两个 table 有多态关联。 (我知道它很乱,但我暂时不得不离开)
在向主 table 查询时,我需要加入两个 table 的结果。我接近我想要的结果,但在我的情况下 master table id returns 是错误的。
数据结构
Table A
|-- id
|-- type
|-- type_id
|-- property_a1
|-- property_a2
Table B
|-- id
|-- property_b1
|-- property_b2
Table C
|-- id
|-- property_c1
|-- property_c2
预期输出
我想要下面的结果。每行包含所有三个 table 的所有字段(或通过查询选择)。如果给定的关联 table 中缺少 属性,则分配 Null 或空字符串。
Result Row
|-- id
|-- property_a1
|-- property_a2
|-- type
|-- type_id
|-- property_b1 // if property from Table c value = ''
|-- property_b2 // if property from Table c value = ''
|-- property_c1 // if property from Table b value = ''
|-- property_c2 // if property from Table b value = ''
我目前的工作可以在这个sqlfiddle:
中看到http://sqlfiddle.com/#!9/2d05e/17
SQL 我使用的查询
来自fiddle。
SELECT
product.id,
res.name,
res.fruit_id,
res.vegetable_id,
res.is_green,
res.color,
product.price,
res.seasonal
FROM
product
JOIN
(
SELECT
fruit.id AS fruit_id,
0 AS vegetable_id,
fruit.name,
fruit.color,
fruit.seasonal,
false as is_green
FROM
fruit
UNION
SELECT
0 as fruit_id,
vegetable.id AS vegetable_id,
vegetable.name,
'' as `color`,
false as `seasonal`,
vegetable.is_green
FROM
vegetable
) res ON product.type_id = res.fruit_id OR product.type_id = res.vegetable_id
我可以使用 GROUP BY
但这只会删除必要的数据。
试试这个
SELECT product.id, res.name, res.fruit_id, res.vegetable_id,
res.is_green,res.color,product.price,res.seasonal
FROM product
JOIN
(
SELECT fruit.id AS fruit_id,
0 AS vegetable_id,fruit.name,fruit.color,fruit.seasonal,
false as is_green
FROM fruit
UNION
SELECT 0 as fruit_id,vegetable.id AS vegetable_id,
vegetable.name,'' as `color`,false as `seasonal`, vegetable.is_green
FROMvegetable
) res ON (product.type_id = res.fruit_id AND product.product_type = 'fruit')
OR (product.type_id = res.vegetable_id AND product.product_type = 'vegetable')