Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL如何使用具有不同联接条件的同一字段?_Mysql_Sql_Concatenation_Concat_Group Concat - Fatal编程技术网

Mysql SQL如何使用具有不同联接条件的同一字段?

Mysql SQL如何使用具有不同联接条件的同一字段?,mysql,sql,concatenation,concat,group-concat,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

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

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)分隔符“,”)shop_name” 店铺名称和供应商来自member.role\u id上的同一字段


有人能帮我吗?我将非常感谢:)

您必须加入
会员
两次。一次根据
水果.vendor\u id
获取供应商名称,另一次根据
shop\u 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

0
之后的
A
应该是什么意思?这看起来像是一个语法错误。
m.id=s.member\u id
将始终为真,因为这是连接中的
ON
条件。@Barmar对不起,我的错,我想我写问题时手指滑倒了,我已经解决了它