简体   繁体   English

SQLite 中的 GROUP_CONCAT

[英]GROUP_CONCAT in SQLite

I am having data like this我有这样的数据

1 A
1 B
1 C
1 D
2 E
2 F
3 G
3 H
3 I
3 J
3 K

by using this query通过使用此查询

select ABSTRACTS_ITEM._id,Name 
from ABSTRACTS_ITEM , ABSTRACT_AUTHOR , AUTHORS_ABSTRACT
where
ABSTRACTS_ITEM._id = AUTHORS_ABSTRACT.ABSTRACTSITEM_ID
and
ABSTRACT_AUTHOR._id = AUTHORS_ABSTRACT.ABSTRACTAUTHOR_ID

Now, I want to show data like this现在,我想显示这样的数据

1 A,B,C,D
2 EF

and so on..I also know it can achieve by GROUP_CONCAT function.等等..我也知道它可以通过GROUP_CONCAT function 来实现。 So, I tried with this所以,我试过这个

SELECT ABSTRACTS_ITEM._id,
GROUP_CONCAT(ABSTRACT_AUTHOR.NAME) FROM
(select ABSTRACTS_ITEM._id,
Name
from
ABSTRACTS_ITEM , ABSTRACT_AUTHOR , AUTHORS_ABSTRACT
where
ABSTRACTS_ITEM._id = AUTHORS_ABSTRACT.ABSTRACTSITEM_ID
and
ABSTRACT_AUTHOR._id = AUTHORS_ABSTRACT.ABSTRACTAUTHOR_ID)

But, It shows me error.但是,它显示了我的错误。 So, what I am doing wrong here.所以,我在这里做错了什么。 What are the right procedure to achieve that?实现这一目标的正确程序是什么?

You need to add GROUP BY clause when you are using aggregate function. 使用聚合函数时,需要添加GROUP BY子句。 Also use JOIN to join tables. 还可以使用JOIN连接表。

So try this: 试试这个:

SELECT AI._id, GROUP_CONCAT(Name) AS GroupedName
  FROM ABSTRACTS_ITEM AI 
  JOIN AUTHORS_ABSTRACT AAB ON AI.ID = AAB.ABSTRACTSITEM_ID
  JOIN ABSTRACT_AUTHOR AAU ON AAU._id = AAB.ABSTRACTAUTHOR_ID
 GROUP BY tbl._id;

See this sample SQLFiddle 请参阅此示例SQLFiddle


What you were trying was almost correct. 你在尝试的几乎是正确的。 You just needed to add GROUP BY clause at the end. 您只需要在末尾添加GROUP BY子句。 But the first one is better. 但第一个更好。

SELECT ID,
GROUP_CONCAT(NAME) 
FROM
    (select ABSTRACTS_ITEM._id AS ID,
     Name
     from
    ABSTRACTS_ITEM , ABSTRACT_AUTHOR , AUTHORS_ABSTRACT
    where
    ABSTRACTS_ITEM._id = AUTHORS_ABSTRACT.ABSTRACTSITEM_ID
    and
    ABSTRACT_AUTHOR._id = AUTHORS_ABSTRACT.ABSTRACTAUTHOR_ID)
GROUP BY ID;

Here my answer to this closed question (it has a good showcase in it).这是我对这个封闭问题的回答(它有一个很好的展示)。 SQL group results by row SQL 按行分组结果

You must add an Aggregate Function to your selected field.您必须将聚合 Function 添加到所选字段。
The one you want is: GROUP_CONCAT with a custom SEPARATOR ', '您想要的是: GROUP_CONCAT与自定义SEPARATOR ', '

See related MySQL Documentation: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html See related MySQL Documentation: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html

Your Select Query would look like this:您的 Select 查询将如下所示:

SELECT id_place, placename, GROUP_CONCAT(plant_name SEPARATOR ', ') AS 'plantnames'
FROM BLOOM
JOIN PLACE ON id_place = fk_place
JOIN PLANT ON id_plant = fk_plant
GROUP BY id_place, placename;

Then you get your desired output:然后你得到你想要的 output:

id_place id_place placename地名 plantnames植物名
1 1 New York纽约 orchid, tulip兰花、郁金香
2 2 London伦敦 rosebush, orchid, tulip蔷薇、兰花、郁金香
3 3 Paris巴黎 rosebush, lily, violet蔷薇、百合、紫罗兰

Here is a DB Fiddle to check this out (MySQL 8.0): https://www.db-fiddle.com/f/sSU7G4kKEaxsLMNweAtLSA/2这是一个数据库小提琴来检查这个(MySQL 8.0): https://www.db-fiddle.com/f/sSU7G4kKEaxsLMNweAtLSA/2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM