我有3个表:MySQL的GROUP_CONCAT加入
作者
author_id last_name first_name organization
1 Smith John Org A
2 Smith Mary Org A
3 Doe John Org B
4 Doe Jane Org C
5 Jones John Org C
6 Sarduchi Guido Org A
author__papers
author_id paper_id is_conctact_author
1 1001 0
2 1001 0
3 1001 0
4 1002 0
5 1002 0
6 1001 1
org_info(这已经过滤的is_contact_author < 1)
paper_id organization idx org_count
1001 Org A 1 2
1001 Org B 2 2
1002 Org C 1 1
我需要的是一个结果集,看起来像这样基于上面的样本数据:
paper_id organization last_names idx org_count
1001 org A Smith, Smith 1 2
1001 org B Doe 2 2
1002 Org C Doe, Jones 1 1
我亲近有:
select AP.paper_id as paper_id, organization
GROUP_CONCAT(last_name SEPARATOR ', '), idx, org_count
from authors A
inner join authors__papers AP ON A.author_id = AP.author_id
inner join org_info O ON O.paper_id = AP.paper_id AND O.organization = A.organization
where AP.is_contact_author < 1
group by O.organization, AP.paper_id
order by paper_id;
...但最后名字被复制。如果我向GROUP_CONCAT添加一个DISTINCT,那么即使它是一个不同的人,我也会忽略姓氏相同的值(例如Smith)。
有关如何最好地采取此示例数据并获得所需输出而不出现问题的任何想法?
TIA!
一般GROUP BY规则说: 如果指定了GROUP BY子句,SELECT列表中的每个列引用必须标识一个分组列或者是一个set函数的参数。 – jarlh
选择 –
中ARE所在的组中的列是,但select中有列(idx,org_count),但不在group by中。 (在较新的MySQL版本中无效,在旧版本中出现不可预知的结果。) – jarlh