group-concat

MySQL group_concat with where clause

若如初见. 提交于 2019-12-04 07:48:43
I got this problem with Group_Concat and a where filter. In my table i got module names which are linked to a client. I want to search clients by module name, but in the group concat i still want to see all modules that are owned by the client. currently it will display all clients with those modules, but it will only display that specific module. I can't figure out how to make them both work together. Any suggestions on how to get my expected result?? These are some basic tables and the query i tried along with results i get and the result i really wanted Client +--------------------+ | id |

Can we define a GROUP_CONCAT function in PostgreSQL?

本小妞迷上赌 提交于 2019-12-04 07:08:30
问题 I have several lines of SQL code for my legacy database with GROUP_CONCAT statements, as in: SELECT SUM(age), GROUP_CONCAT(sal) FROM Users; In PostgreSQL, I can do the same with: SELECT SUM(age), string_agg(sal, ', ') FROM Users; I would like to reuse the old SQL as much as possible. So I need to define a GROUP_CONCAT function that internally calls string_agg . Is this possible? EDIT: The linked question is unrelated! My question asks "How to define a function called group_concat?". The

mysql GROUP_CONCAT

*爱你&永不变心* 提交于 2019-12-04 05:29:43
I want to list all users with their corropsonding user class. Here are simplified versions of my tables CREATE TABLE users ( user_id INT NOT NULL AUTO_INCREMENT, user_class VARCHAR(100), PRIMARY KEY (user_id) ); INSERT INTO users VALUES (1, '1'), (2, '2'), (3, '1,2'); CREATE TABLE classes ( class_id INT NOT NULL AUTO_INCREMENT, class_name VARCHAR(100), PRIMARY KEY (class_id) ); INSERT INTO classes VALUES (1, 'Class 1'), (2, 'Class 2'); And this is the query statement I am trying to use but is only returning the first matching user class and not a concatenated list as hoped. SELECT user_id,

GROUP_CONCAT change GROUP BY order

霸气de小男生 提交于 2019-12-04 03:40:35
问题 I have a VIEW (lots of joins) that outputs data ordered by a date ASC . Works as expected. OUTPUT similar to: ID date tag1 other_data 1 25-03-2011 blue fff <= 1 26-03-2011 red ggg 1 27-03-2011 pink yyy 2 25-03-2011 red yyy <= 2 26-03-2011 orange rrr If I apply a GROUP BY ID . For the other columns MySQL outputs the first found row of each ID. I read this somewhere in te docs. SELECT * FROM `myVIEW` GROUP BY `ID` ID date tag1 other_data 1 25-03-2011 blue fff <= 2 25-03-2011 red yyy <= Now lets

MySQL: combining multiple values after a join into one result column

浪尽此生 提交于 2019-12-04 01:43:41
I have a database with a table for publications, each of which can have multiple authors that are stored in a different table. I'd like to query the database into giving me a list of publication titles in one column, and the combined authors for that publication in the second. SELECT p.`id`, p.`title`, a.`fullname` from `publications` p LEFT JOIN `authors` a on a.`publication_id` = p.`id`; This of course gives me multiple times the publication title for as many authors. id title fullname -- ----- -------- 1 Beneath the Skin Sean French 1 Beneath the Skin Nicci Gerrard 2 The Talisman Stephen

How to execute a query which is stored in a table column MySQL?

让人想犯罪 __ 提交于 2019-12-03 16:31:22
mysql> select * from CT; | CID | MID | REPORT_QUERY | | 1 | 1 | select * from emp; | | 2 | 2 | select * from student; | 2 rows in set (0.00 sec) I want to execute queries in REPORT_QUERY column. DELIMITER // CREATE PROCEDURE TRYct() BEGIN SET @str=(SELECT GROUP_CONCAT(REPORT_QUERY SEPARATOR ' ') FROM CT); PREPARE q from @str; EXECUTE q; END // DELIMITER ; i use this code but it works if there is only one query in my table. if there is two query than it gives an error. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for

SQL GROUP_CONCAT split in different columns

旧巷老猫 提交于 2019-12-03 14:46:33
I searched a lot, but didn't find a proper solution to my problem. What do I want to do? I have 2 tables in MySQL: - Country - Currency (I join them together via CountryCurrency --> due to many to many relationship) See this for a working example: http://sqlfiddle.com/#!2/317d3/8/0 I want to link both tables together using a join, but I want to show just one row per country (some countries have multiple currencies, so that was the first problem). I found the group_concat function: SELECT country.Name, country.ISOCode_2, group_concat(currency.name) AS currency FROM country INNER JOIN

Using GROUP_CONCAT on subquery in MySQL

ぃ、小莉子 提交于 2019-12-03 06:29:01
问题 I have a MySQL query in which I want to include a list of ID's from another table. On the website, people are able to add certain items, and people can then add those items to their favourites. I basically want to get the list of ID's of people who have favourited that item (this is a bit simplified, but this is what it boils down to). Basically, I do something like this: SELECT *, GROUP_CONCAT((SELECT userid FROM favourites WHERE itemid = items.id) SEPARATOR ',') AS idlist FROM items WHERE

MySQL get first non null value after group by

此生再无相见时 提交于 2019-12-02 20:14:08
I have a large table with data that is not unique but needs to be. This table is a result of multiple union selects so is not an actual table. I cannot make it an actual table for other reasons. All of the UNION'd tables have an email column which will eventually be unique. The resulting records look like this: 1 ozzy@test.com Ozzy 2 test@test.com Tony 3 test@yahoo.com Steve 4 tiny@test.com 13 tony@gmail.com Tony 14 test@test.com Ozzy 15 test@yahoo.com Dave 16 tiny@test.com Tim As you can see, some emails appear more then once with different names or non existent names. When I add a GROUP BY

Using GROUP_CONCAT on subquery in MySQL

☆樱花仙子☆ 提交于 2019-12-02 20:04:15
I have a MySQL query in which I want to include a list of ID's from another table. On the website, people are able to add certain items, and people can then add those items to their favourites. I basically want to get the list of ID's of people who have favourited that item (this is a bit simplified, but this is what it boils down to). Basically, I do something like this: SELECT *, GROUP_CONCAT((SELECT userid FROM favourites WHERE itemid = items.id) SEPARATOR ',') AS idlist FROM items WHERE id = $someid This way, I would be able to show who favourited some item, by splitting the idlist later