Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate/join strings from a grouped column in SQLite?

Tags:

Having

Company id      Name 1       Enron 2       Walmart  Employee id      Company 2       1 3       1 4       2 5       2 6       2

I want to get

Enron 2,3 Walmart 4,5,6

so far I wrote:

select Company.Name, Employee.id from Company inner join Employee on Company.id = Employee.Company  group by Company.id 

but the current result is

Enron 2 Walmart 4
like image 328
Jader Dias Avatar asked Apr 18 '11 19:04

Jader Dias


People also ask

How can we concatenate string in SQLite?

The SQL standard provides the CONCAT() function to concatenate two strings into a single string. SQLite, however, does not support the CONCAT() function. Instead, it uses the concatenate operator ( || ) to join two strings into one.

How do I join columns in SQLite?

The syntax for the SQLite CROSS JOIN is: SELECT columns FROM table1 CROSS JOIN table2; NOTE: Unlike an INNER or OUTER join, a CROSS JOIN has no condition to join the 2 tables.

Can we use concat with group by in SQL?

MySQL | Group_CONCAT() Function. The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.

How do I concatenate a column in a string in SQL?

To append a string to another and return one result, use the || operator. This adds two strings from the left and right together and returns one result. If you use the name of the column, don't enclose it in quotes. However, in using a string value as a space or text, enclose it in quotes.


1 Answers

Use Group_Concat:

select Company.Name, Group_Concat(Employee.id) from Company inner join Employee on Company.id = Employee.Company  group by Company.id 
like image 100
MPelletier Avatar answered Sep 17 '22 14:09

MPelletier