IT Community - Software Programming, Web Development and Technical Support

group_concat in MySQL

This is a discussion on group_concat in MySQL within the Database Support forums, part of the Web Development category; Hi, Is it possible to use more group_concat in a single select query in MySql?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  6 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 05-23-2007, 06:16 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default group_concat in MySQL

Hi,

Is it possible to use more group_concat in a single select query in MySql?
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-24-2007, 05:51 AM
vadivelanvaidyanathan vadivelanvaidyanathan is offline
D-Web Genius
 
Join Date: Feb 2007
Posts: 803
vadivelanvaidyanathan is on a distinguished road
Default Re: Use on group_concat

Yeah.. Its possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-10-2007, 08:24 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: Use on group_concat

Hi,

You can use any number of group_concat function in a select query but the result value depends of the system variable
group_concat_max_len. The default values for this is 1024 (1 kb). If you want to change the the length of that system
variable use can use the below given.

To view the current the length of the group_concat function.
mysql> SHOW VARIABLES LIKE 'group%';
Variable_name Value
-------------------- ------
group_concat_max_len 1024

to change the length of the group_concat function use,
mysql> SET @@group_concat_max_len = 524288; -- 0.5 MB

Now check the length of the variable,
mysql>SHOW VARIABLES LIKE 'group%';
Variable_name Value
-------------------- ------
group_concat_max_len 524288

The value can be set higher, although the maximum effective length of the return value is constrained
by the value of max_allowed_packet. The default value for varialbe max_allowed_packet is 1 mb. You can change value
of this variable also.

To change the value of the max_allowed_packet size, first need to modify this value in the config (my.cnf or .ini) file as given below..

# Set max allowed packet size to 2 MB
max_allowed_packet=2M

Restart the mysql service and the system variable as given below.

mysql>SHOW VARIABLES LIKE 'max_allowed_packet';
mysql>SET @@ max_allowed_packet = 2097152; -- 2 mb, value in bytes..

Now the max_allowed_packet value changed to 2 mb.
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-11-2007, 02:50 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Use on group_concat

Hi,

While using group_concat in my query,the result set cannot be viewed properly.

Iam using TOAD for MYSQL editor for my MYSQL Database.

It displays System.Byte[], this is due to problem in the TOAD for MYSQL or need to change values for any server side variables?
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-11-2007, 04:51 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: Use on group_concat

Hi Murali,
Generaly group_concat functions returns the result in blob format. It seems like like toad error / may not a error also. Anyway, if you want to view the result set in toad using group_concat function means use CAST function and convert the group concat result set into binary.

Eg..
SELECT CAST(group_concat(emp_id) AS BINARY) FROM emp_master;
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-11-2007, 06:28 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Use on group_concat

Again an useful one ....
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-16-2007, 10:39 PM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Question Re: Use on group_concat

Hi,

Please hel to me, GROUP_CONCAT function is available in MS SQL SERVER and ORACLE?

-R.Gopi
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-16-2007, 11:29 PM
prasath prasath is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: Chennai
Posts: 173
prasath is on a distinguished road
Default Re: Use on group_concat

Hi,
Group_concat is avilable in Mysql server only,but oracle has equalient group concat functionality that achived in simple query.it is given as below


CREATE OR REPLACE TYPE ntt_varchar2 AS TABLEOF VARCHAR2(4000);
select deptno, cast(collect(ename) as ntt_varchar2) as vals from emp group by deptno
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-17-2007, 12:40 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: Use on group_concat

Hi Prasad,

As you said there is no pre definied function (like GROUP_CONCAT in mysql) available in MS-SQL or Oracle.

for MS-SQL we need to use stored function / precedure to get the result as desired. There is no direct function available for group_concat in oracle also.

CREATE OR REPLACE TYPE ntt_varchar2 AS TABLEOF VARCHAR2(4000);
select deptno, cast(collect(ename) as ntt_varchar2) as vals from emp group by deptno

I tried your points in oracle, but it throws error like this...
ORA-00904: "COLLECT": invalid identifier

Can u please clarify ?
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 07-18-2007, 03:22 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Use on group_concat

Hi Priyan,

May be running the query in a lower version.
COLLECT Operator is available in ORACLE 10g Version.

Check Your Version

SELECT * FROM V$VERSION;

Try the below in ORACLE-10g

SELECT * FROM V$VERSION;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production

CREATE OR REPLACE TYPE Cnvt_Type IS TABLE OF VARCHAR2(100);

CREATE TABLE TEST_VALUE(ID NUMBER,NAME VARCHAR(100));

INSERT INTO TEST_VALUE VALUES(1,'TEST');
INSERT INTO TEST_VALUE VALUES(2,'TEST1');
INSERT INTO TEST_VALUE VALUES(3,'TEST2');
INSERT INTO TEST_VALUE VALUES(4,'TEST3');

SELECT CAST(COLLECT(NAME) AS Cnvt_Type)AS Records FROM TEST_VALUE;

Records
--------
TEST
TEST1
TEST2
TEST3

Prasath,

By using Collection Method as per your idea, it will return the result in different rows but using the GROUP_CONCAT() function in MYSQL will return the record set in a single row with comma seperator.
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 07-18-2007, 04:38 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: Use on group_concat

Yes Murali, i tried that in lower version. ....

But there is someother way to get the result, this is somewhat similar to MySQL group_concat. It's seems to a big query.. just try this..

-- Create table for manipulation....

CREATE TABLE EMP (ID NUMBER, ENAME VARCHAR2(30), DEPT_ID INTEGER);
INSERT INTO EMP VALUES(1,'Raj', 10);
INSERT INTO EMP VALUES(2,'Kumar', 20);
INSERT INTO EMP VALUES(3,'Selvam', 10);
INSERT INTO EMP VALUES(4,'John', 10);
INSERT INTO EMP VALUES(5,'Smith', 10);

-- Query...

WITH DATA
AS
(
SELECT dept_id, ename, ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY ename) rnum,
COUNT(*) OVER (PARTITION BY dept_id) count FROM emp
)
SELECT dept_id, ltrim(sys_connect_by_path(ename,','),',') Result_String
FROM DATA WHERE rnum = count
start with rnum = 1
CONNECT BY PRIOR dept_id = dept_id and prior rnum = rnum-1
ORDER BY dept_id;


--------------------------------------------------------------------------------
DEPT_ID RESULT_STRING
--------------------------------------------------------------------------------
10 John,Raj,Selvam,Smith

20 Kumar
--------------------------------------------------------------------------------

However, this is not the same result as we get in mysql using the below mysql query.

SELECT group_concat(ename) as Result_String FROM EMP;

Result_String
---------------------------------------
John,Raj,Selvam,Smith,Kumar
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 07-19-2007, 07:59 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Use on group_concat

Hi Priyan,

Yes its not similar to MYSQL GROUP_CONCAT().

Not a simple one using SELECT Query but the other way using a PL/SQL Block we can get the same result as per the MYSQL GROUP_CONCAT().

PHP Code:
SET SERVEROUTPUT ON;
DECLARE
vName          VARCHAR(100);
BEGIN
FOR I IN(SELECT ENAME FROM EMP)
LOOP
  
IF(vName IS NULL)THEN
     vName 
:= I.ENAME;
  ELSE
     
vName := CONCAT(CONCAT(vName,','),I.ENAME);
  
END IF;
END LOOP;  
DBMS_OUTPUT.PUT_LINE('The Names From Emp Table:'||vName);
END
HTML Code:
Output:
The Names From Emp Table: John,Raj,Selvam,Smith,Kumar.
PL/SQL procedure successfully completed.
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 07-21-2007, 06:25 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: Use on group_concat

Hi techies,

There is a new issue here about MySQL GROUP_CONCAT() function.

when i try to use the group_concat() function's result value with IN() Condition, i am not getting expected result set. Please find the ex given below.

eg.
SELECT dept_id, dept_name FROM dept_id
WHERE dept_id IN(SELECT GROUP_CONCAT(emp_dept_id) FROM emp_table)


This query is working but in the where condition, say for example the subquery returns (10,20,30,50) means it takes only the first value (10).

If any one come across this issue please reply the solution...
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 07-24-2007, 04:27 AM
Selena Selena is offline
D-Web Trainee
 
Join Date: Jul 2007
Location: Bangalore.
Posts: 9
Selena is on a distinguished road
Default Re: Use on group_concat

Hi,

As iam new to Database and atpresent working in ORACLE.
But i heard Oracle and Mysql differ with slight syntax only, so i will try this and tell you if i find any solutions to this.
__________________
Selna.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 07-24-2007, 05:08 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Use on group_concat

Hi Priyan/Selena,

Try this one,may be this could help u to solve your GROUP_CONCAT() Issue,

Quote:

DROP PROCEDURE IF EXISTS sp_test_groupconcat;
CREATE PROCEDURE sp_test_groupconcat()
NOT DETERMINISTIC
SQL SECURITY DEFINER
COMMENT 'Just to Check the GROUP_CONCAT() Issue'
sp_test_groupconcat:
BEGIN
DECLARE vQuery TEXT;
DECLARE vQuery1 TEXT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN ROLLBACK;END;

SET vQuery:= 'SELECT CAST(GROUP_CONCAT(table_id)AS BINARY) INTO @vId FROM table1';
SET @vQuery := vQuery;
-- SELECT @vQuery;
PREPARE vSqlQuery FROM @vQuery;
EXECUTE vSqlQuery;
-- DEALLOCATE PREPARE vSqlQuery;
SET vQuery1:= CONCAT('SELECT * FROM table2 WHERE table1_id IN (',@vId ,')');
SET @vQuery1 := vQuery1;
SELECT @vQuery1;
PREPARE vSqlQuery1 FROM @vQuery1;
EXECUTE vSqlQuery1;
DEALLOCATE PREPARE vSqlQuery1;
END sp_test_groupconcat;

CALL sp_test_groupconcat();
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 07-25-2007, 08:39 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: Use on group_concat

Hi Murali,

Yes, it's working with dynamic query but it will make impact on the query performance. Your SP handles the numerics in the group concat function. It would be more complicated if we handling strings in the group concat function with dynamic query.

There must be some solution for this issue !!!
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 07-26-2007, 04:47 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Use on group_concat

Hi Priyan,

Have u noticed one thing.Try this

CREATE TABLE testing(id INTEGER,name VARCHAR(10));
INSERT INTO testing VALUES (1,'1000,1010');
SELECT * FROM table_name WHERE table_id IN (SELECT name FROM testing);

The above query returns only one record from the table as per the first value from the field.

This is because the difference as follows

SELECT * FROM table_name WHERE table_id IN ('1000,1010'), trims the string to the INTEGER and takes just the first value.so the query will work as
SELECT * FROM table_name WHERE table_id IN ('1000');

Might be the same thing happens in the GROUP_CONCAT().

As you said that it will be very tedious for handling strings, each values has to be concated as a string and to get the result from the query.

Hope believe there will be more flexible String Functions are expected in MYSQL5.1.
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18 (permalink)  
Old 07-30-2007, 06:32 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Use on group_concat

Hi,

Have u noticed one thing,

Code:
SELECT GROUP_CONCAT(Field_name) FROM table_name LIMIT 0,10;
But didn't get the expected result?
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19 (permalink)  
Old 08-01-2007, 07:48 AM
Selena Selena is offline
D-Web Trainee
 
Join Date: Jul 2007
Location: Bangalore.
Posts: 9
Selena is on a distinguished road
Default Re: Use on group_concat

Hi murali,

I have tried your sp but have modified it as per my format of variable dec.

Here is the code,

drop procedure if exists sp_test;
create procedure sp_test()
sp_test:
begin
declare var_query text;
declare var_id integer;
SET var_query:= 'SELECT CAST(GROUP_CONCAT(mytable_id)AS BINARY) INTO var_Id FROM mytable';
prepare pre_query FROM var_query;
execute pre_query;
select var_id;
end sp_test;

I got the following Error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'var_query; execute pre_query;select var_id;end sp_test' at line 7

I have checked for my version and its
select version();
5.0.22-max-log

Could u help me what i have missed out here?
__________________
Selna.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #20 (permalink)  
Old 08-02-2007, 02:19 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Use on group_concat

Hi,

The problem is u cannot use the declared variable as in prepare statement command.

drop procedure if exists sp_test;
create procedure sp_test()
sp_test:
begin
declare var_query text;
declare var_id integer;
SET var_query:= 'SELECT CAST(GROUP_CONCAT(mytable_id)AS BINARY) INTO var_Id FROM mytable';
SET @var_query := var_query;
prepare pre_query FROM @var_query;
execute pre_query;
select var_id;
end sp_test;

CALL sp_test();

Now again u will get an error Undeclared variable: var_Id;, indicates that the dynamic sql query can save the value in the global/session variable inside the single query, i.e
'SELECT CAST(GROUP_CONCAT(mytable_id)AS BINARY) INTO @var_Id FROM mytable';

Can't be like this
'SELECT CAST(GROUP_CONCAT(mytable_id)AS BINARY) INTO var_Id FROM mytable';

So here is the code,

Quote:
drop procedure if exists sp_test;
create procedure sp_test()
sp_test:
begin
declare var_query text;
declare var_id integer;
SET var_query:= 'SELECT CAST(GROUP_CONCAT(mytable_id)AS BINARY) INTO @var_Id FROM mytable';
SET @var_query := var_query;
prepare pre_query FROM @var_query;
execute pre_query;
select @var_id;
end sp_test;
Quote:
Output:
1,2,3
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/database-support/1401-group_concat-mysql.html
Posted By For Type Date
Group_Concat in MYSQL: Group_Concat in MYSQL This thread Refback 11-09-2007 05:40 AM
distinct with multiple field - MySQL Forum This thread Refback 09-26-2007 03:52 AM
Group_Concat in MYSQL This thread Refback 09-12-2007 01:09 AM
distinct with multiple field - MySQL Forum