group-concat

tsql aggregate string for group by

穿精又带淫゛_ 提交于 2019-11-29 12:50:15
I have two tables: Names(id, name) Addresses(id, name_id, address) I want to write query that return me: name, address list (address1, address2, adress3, ..) Something like: Select A.name, B.list_of_addresses From Names A Inner Join (Select name_id, /*list_of_addresses with comma between them*/ From Addresses Group By name_id) B ON A.id=B.name_id You can use For XML as a trick to achieve that from SQL Server 2005 onwards. Select A.name, stuff(( select ',' + B.address from Addresses B WHERE A.id=B.name_id for xml path('')),1,1,'') From Names A It works well if you don't already have commas in

CommunicationsException: Communications link failure

一笑奈何 提交于 2019-11-29 11:17:55
I used java to query some records from Mysql. But in some querys of one duration, i meet a problem which make query failed, but in others , it query successful. The error message is next: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet successfully received from the server was 90 milliseconds ago. The last packet sent successfully to the server was 1,674 milliseconds ago. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at

Group Concat Results Cut Off

雨燕双飞 提交于 2019-11-29 06:58:58
问题 I'm using the following query and it's returning the expected results.. except for the fact that the concatenated results appear to be cut off. Example: "1965 Chevrolet Suburban, 1958 Chevrolet Bel Air, 1969 Chevrolet K20 Suburban, 1967 Chevrolet Bel Air, 1964 Chevrolet C10 Pickup, 1970 Chevrolet G10 Van, 1969 Chevrolet K20 Pickup, 1965 Chevrolet Biscayne, 1970 Chevrolet Brookwood, 1964 Chevrolet P30 Series, 1966 Chevrolet Bel Air, 1967 Chevrolet C20 Pickup, 1972 Chevrolet Blazer, 1961

LEFT JOIN after GROUP BY?

醉酒当歌 提交于 2019-11-29 04:21:21
I have a table of "Songs", "Songs_Tags" (relating songs with tags) and "Songs_Votes" (relating songs with boolean like/dislike). I need to retrieve the songs with a GROUP_CONCAT() of its tags and also the number of likes (true) and dislikes (false). My query is something like that: SELECT s.*, GROUP_CONCAT(st.id_tag) AS tags_ids, COUNT(CASE WHEN v.vote=1 THEN 1 ELSE NULL END) as votesUp, COUNT(CASE WHEN v.vote=0 THEN 1 ELSE NULL END) as votesDown, FROM Songs s LEFT JOIN Songs_Tags st ON (s.id = st.id_song) LEFT JOIN Votes v ON (s.id=v.id_song) GROUP BY s.id ORDER BY id DESC The problem is that

Multiple GROUP_CONCAT on different fields using MySQL

懵懂的女人 提交于 2019-11-29 01:16:04
I have a query like this: SELECT product.id, GROUP_CONCAT(image.id) AS images_id, GROUP_CONCAT(image.title) AS images_title, GROUP_CONCAT(facet.id) AS facets_id ... GROUP BY product.id And the query works, but not as expected, because if I have a product with 5 facets and 1 image (suppose an image with id=7), then I get something like this in "images_id": "7,7,7,7,7" If I have 2 images (7 and 3) then I get something like: "7,7,7,7,7,3,3,3,3,3" and in facets I get something like: "8,7,6,5,4,8,7,6,5,4" I think MySQL is making some type of union of the differents rows returned by the query, and

JOIN and GROUP_CONCAT with three tables

こ雲淡風輕ζ 提交于 2019-11-28 21:37:54
I have three tables: users: sports: user_sports: id | name id | name id_user | id_sport | pref ---+-------- ---+------------ --------+----------+------ 1 | Peter 1 | Tennis 1 | 1 | 0 2 | Alice 2 | Football 1 | 2 | 1 3 | Bob 3 | Basketball 2 | 3 | 0 3 | 1 | 2 3 | 3 | 1 3 | 2 | 0 The table user_sports links users and sports with an order of preference ( pref ). I need to make a query that returns this: id | name | sport_ids | sport_names ---+-------+-----------+---------------------------- 1 | Peter | 1,2 | Tennis,Football 2 | Alice | 3 | Basketball 3 | Bob | 2,3,1 | Football,Basketball,Tennis I

How do I concatenate strings from a subquery into a single row in mysql?

岁酱吖の 提交于 2019-11-28 17:13:34
I have three tables: table "package" ----------------------------------------------------- package_id int(10) primary key, auto-increment package_name varchar(255) price decimal(10,2) table "zones" ------------------------------------------------------ zone_id varchar(32) primary key (ex of data: A1, Z2, E3, etc) table "package_zones" ------------------------------------------------------ package_id int(10) zone_id varchar(32) What I'm trying to do is return all the information in package table PLUS a list of zones for that package. I want the list of zones sorted alphabetically and comma

MySQL Group_Concat Repeating Values

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 11:09:55
I am working on an open source project called PHP-Bouncer , and I'm having issues with a MySQL Query I am writing for it. Basically we have three tables: BouncerRoles, PageInRole, and BouncerPageOverrides. BouncerRoles contains access levels, and the other two tables link back to BouncerRoles via Foreign Key and provide multiple entries of additional data. I have written the following query to attempt to pull all of the role data I need all at once: select BouncerRoles.RoleID, BouncerRoles.RoleName, GROUP_CONCAT(PageInRole.PageName separator '|') as ProvidedPages, GROUP_CONCAT(CONCAT

MySQL GROUP_CONCAT escaping

回眸只為那壹抹淺笑 提交于 2019-11-28 06:59:16
(NOTE: This question is not about escaping queries, it's about escaping results) I'm using GROUP_CONCAT to combine multiple rows into a comma delimited list. For example, assume I have the two (example) tables: CREATE TABLE IF NOT EXISTS `Comment` ( `id` int(11) unsigned NOT NULL auto_increment, `post_id` int(11) unsigned NOT NULL, `name` varchar(255) collate utf8_unicode_ci NOT NULL, `comment` varchar(255) collate utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`), KEY `post_id` (`post_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ; INSERT INTO `Comment` (`id`,

tsql aggregate string for group by

守給你的承諾、 提交于 2019-11-28 06:35:20
问题 I have two tables: Names(id, name) Addresses(id, name_id, address) I want to write query that return me: name, address list (address1, address2, adress3, ..) Something like: Select A.name, B.list_of_addresses From Names A Inner Join (Select name_id, /*list_of_addresses with comma between them*/ From Addresses Group By name_id) B ON A.id=B.name_id 回答1: You can use For XML as a trick to achieve that from SQL Server 2005 onwards. Select A.name, stuff(( select ',' + B.address from Addresses B