SQL如何使用具有不同连接条件的相同字段?

时间:2017-03-02 22:11:35

标签: mysql sql concatenation concat group-concat

好的,我遇到了这个问题。我有以下表格:

member
id  | username      | role_id | full_name       |
1   | abc@email.com |   1     | administrator   |
2   | bcd@email.com |   2     | Sunkist         |
3   | cde@email.com |   2     | BlueJam         |
4   | def@email.com |   3     | Fresh Shop      |
5   | efg@email.com |   3     | Other Shop      |

role
id  | role          |
1   | superadmin    |
2   | vendor        |
3   | shop          |

fruits
id  | fruit_name    |   barcode | vendor_id |
1   | banana        |   12345   |    2      |
2   | melon         |   23456   |    2      |
3   | apel          |   34567   |    3      |
4   | orange        |   45678   |    3      |
5   | papaya        |   56789   |    2      |

shop_base
id  | fruit_id  | member_id |
1   |    1      |   4       |
2   |    1      |   5       |
3   |    2      |   4       |
4   |    2      |   5       |
5   |    3      |   5       |
6   |    4      |   5       |
7   |    5      |   5       |

我对此查询很满意:

SELECT f.barcode, f.fruit_name, m.full_name AS vendor, f.id AS fruit_id
FROM fruits AS f
LEFT JOIN member AS m ON f.vendor_id = m.id
WHERE f.vendor_id > 0 
GROUP BY f.barcode
ORDER BY f.barcode DESC

结果:

barcode | fruit_name    | vendor    | fruit_id  |
56789   | papaya        | Sunkist   | 5         |
45678   | orange        | BlueJam   | 4         |
34567   | apel          | BlueJam   | 3         |
23456   | melon         | Sunkist   | 2         |
12345   | banana        | Sunkist   | 1         |

但现在我需要像这样添加商店栏目:

barcode | fruit_name    | vendor    | fruit_id  | shop_name              |
56789   | papaya        | Sunkist   | 5         | Other Shop             |
45678   | orange        | BlueJam   | 4         | Other Shop             |
34567   | apel          | BlueJam   | 3         | Other Shop             |
23456   | melon         | Sunkist   | 2         | Fresh Shop, Other Shop |
12345   | banana        | Sunkist   | 1         | Fresh Shop, Other Shop |

这是我到目前为止的方式,但它总是在shop_name字段上返回null:

SELECT f.barcode, f.fruit_name, m.full_name AS vendor, f.id AS fruit_id, GROUP_CONCAT(CONCAT(CASE WHEN m.id = s.member_id THEN m.full_name END) SEPARATOR ', ') shop_name
FROM fruits AS f
LEFT JOIN member AS m ON f.vendor_id = m.id
LEFT JOIN shop_base AS s ON m.id = s.member_id
WHERE f.vendor_id > 0 
GROUP BY f.barcode
ORDER BY f.barcode DESC

我认为问题在于:“GROUP_CONCAT(CONCAT(例如,m.id = s.member_id,然后m.full_name END)SEPARATOR',')shop_name” shop_name和vendor来自member.role_id

上的相同字段 任何人都可以帮助我吗?我将非常感谢:)

1 个答案:

答案 0 :(得分:1)

您必须两次加入member。一旦根据fruits.vendor_id获取供应商名称,并单独获取基于shop_base的展示名称。

SELECT f.barcode, f.fruit_name, m.full_name AS vendor, f.id AS fruit_id, GROUP_CONCAT(m1.full_name SEPARATOR ', ') shop_name
FROM fruits AS f
LEFT JOIN member AS m ON f.vendor_id = m.id
LEFT JOIN shop_base AS s ON f.id = s.fruit_id
LEFT JOIN member AS m1 ON s.member_id = m1.id
WHERE f.vendor_id > 0
GROUP BY f.barcode
ORDER BY f.barcode DESC

DEMO