如何查找以下情况的联接查询

时间:2017-07-24 08:25:57

标签: php mysql html5

我有两个名为branchsource的表。表格branch包含列b_idb_name,表格source包含列s_nameb_code,其中每行包含多个b_code被逗号隔开。我想要字段s_nameb_name的结果。

enter image description here

4 个答案:

答案 0 :(得分:0)

SQL 将是这样的:

SELECT source.s_name, branch.b_id
FROM source
INNER JOIN branch ON source.b_code = branch.b_id
ORDER BY source.b_code

我正在使用 MS SQL ,但尚未对该查询进行测试。如果结果是正确的,请告诉我。

答案 1 :(得分:0)

您可以使用FIND_IN_SET尝试以下解决方案:

SELECT s.s_name, GROUP_CONCAT(b.b_name) AS 'b_name'
FROM `source` s INNER JOIN branch b ON FIND_IN_SET(b.b_id, s.b_code)
GROUP BY s.s_name
  

演示: http://sqlfiddle.com/#!9/b7d7d1/4/0

答案 2 :(得分:0)

试试这个

SELECT source.*,GROUP_CONCAT(branch.b_name) FROM source LEFT JOIN 
branch ON FIND_IN_SET(branch.b_id,source.b_code) group by source.s_name 

答案 3 :(得分:0)

SELECT source.s_name,GROUP_CONCAT(branch.b_name) 
FROM source 
INNER JOIN branch on FIND_IN_SET(branch.b_id,source.b_code) 
GROUP BY source.s_name;

尝试此查询。