473,472 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Comma Separated

65 New Member
Hi friends,
I need to create a table which refers the fields from an existing table as follows:

Expand|Select|Wrap|Line Numbers
  1. Create table a (select content,sid, count(*) count from b group by content order by count desc limit 0,30)
This code works well and creates a table.
But the content field having different sid's for each value.
ex:

content sid
Apple 3
Apple 5
Apple 8
Mango 7
Mango 10.....

Is there any way to store the sid in comma separated values in mysql?

Like as follows:

content sid
Apple 3,5,8
Mango 7,10.....

Please, help me out.........

Thanx n Regards
Yas.......
Mar 26 '08 #1
15 3925
chaarmann
785 Recognized Expert Contributor
Hi friends,
I need to create a table which refers the fields from an existing table as follows:

Expand|Select|Wrap|Line Numbers
  1. Create table a (select content,sid, count(*) count from b group by content order by count desc limit 0,30)
This code works well and creates a table.
But the content field having different sid's for each value.
ex:

content sid
Apple 3
Apple 5
Apple 8
Mango 7
Mango 10.....

Is there any way to store the sid in comma separated values in mysql?

Like as follows:

content sid
Apple 3,5,8
Mango 7,10.....

Please, help me out.........

Thanx n Regards
Yas.......
You must join many records into one, and you can only do that with the group-by function. If you have a small and limited maximum number of each fruit, you can do that with a normal SQL, but it's very tricky and lengthy. better you use a stored procedure for it.

But if you have a small maximum number of each fruit, for example 3, then you can do with normal SQL:

Expand|Select|Wrap|Line Numbers
  1. create table fruits as (select content, null as sids from a group by content);
  2. insert into fruits set sids=
  3. concat(
  4. (select sid from a where a.content = sid.content limit 0,1) , ',',
  5. (select sid from a where a.content = sid.content limit 1,1) , ',',
  6. (select sid from a where a.content = sid.content limit 2,1) , ',',
  7. );
I hope you get the idea.

At the end, you can run an SQL to replace multiple occurences of ',,' with an empty string to delete unnecessary commas at the end in 'sids' column.
Mar 26 '08 #2
mwasif
802 Recognized Expert Contributor
Try this query
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE a (SELECT content,GROUP_CONCAT(sid) sid, count(*) count FROM b GROUP BY content ORDER BY count desc LIMIT 0,30)
Mar 26 '08 #3
chaarmann
785 Recognized Expert Contributor
Try this query
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE a (SELECT content,GROUP_CONCAT(sid) sid, count(*) count FROM b GROUP BY content ORDER BY count desc LIMIT 0,30)
group_concat is availabe only since mySql version 4.1 and above.
Mar 26 '08 #4
ronverdonk
4,258 Recognized Expert Specialist
group_concat is availabe only since mySql version 4.1 and above.
So why don't we ask yasmine what version of MyQL she's got? Yasmine?

Ronald
Mar 26 '08 #5
yasmine
65 New Member
So why don't we ask yasmine what version of MyQL she's got? Yasmine?

Ronald
yah....
i'm having the mysql version 4.1.11.

Thanks for the coding

This query works well and creates a table. But the survey id field does not contain comma separated values.
The sid field contains only [BLOB - 1.0 KB] values for all the rows.
I don't know what is it.
Can u help me.........????

Thanx n Regards
Yas........
Mar 27 '08 #6
yasmine
65 New Member
You must join many records into one, and you can only do that with the group-by function. If you have a small and limited maximum number of each fruit, you can do that with a normal SQL, but it's very tricky and lengthy. better you use a stored procedure for it.

But if you have a small maximum number of each fruit, for example 3, then you can do with normal SQL:

Expand|Select|Wrap|Line Numbers
  1. create table fruits as (select content, null as sids from a group by content);
  2. insert into fruits set sids=
  3. concat(
  4. (select sid from a where a.content = sid.content limit 0,1) , ',',
  5. (select sid from a where a.content = sid.content limit 1,1) , ',',
  6. (select sid from a where a.content = sid.content limit 2,1) , ',',
  7. );
I hope you get the idea.

At the end, you can run an SQL to replace multiple occurences of ',,' with an empty string to delete unnecessary commas at the end in 'sids' column.
Hi chaarmann,
Thanks for ur coding.
But i'm having very large amount of entries in my table.
So, i can't do it.
anyway,
Thanx n Regards
Yas.......
Mar 27 '08 #7
taheer123
1 New Member
good post
fantastic
Mar 27 '08 #8
write2ashokkumar
39 New Member
Hi friends,
I need to create a table which refers the fields from an existing table as follows:

Expand|Select|Wrap|Line Numbers
  1. Create table a (select content,sid, count(*) count from b group by content order by count desc limit 0,30)
This code works well and creates a table.
But the content field having different sid's for each value.
ex:

content sid
Apple 3
Apple 5
Apple 8
Mango 7
Mango 10.....

Is there any way to store the sid in comma separated values in mysql?

Like as follows:

content sid
Apple 3,5,8
Mango 7,10.....

Please, help me out.........

Thanx n Regards
Yas.......



Hi,

Yes, we can store the values as comma separated value. Following example explain you properly and its very simple to understand... i hope...

Example:


-- Create table
-- ============

mysql> create table content(content varchar(100),sid varchar(100));
Query OK, 0 rows affected (0.04 sec)

-- Insert the values to the table
-- ==============================

mysql> insert into content values('apple',1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into content values('apple',2);
Query OK, 1 row affected (0.00 sec)

mysql> insert into content values('apple',3);
Query OK, 1 row affected (0.00 sec)

mysql> insert into content values('apple',4);
Query OK, 1 row affected (0.00 sec)

mysql> insert into content values('apple',5);
Query OK, 1 row affected (0.00 sec)

mysql> insert into content values('mango',1);
Query OK, 1 row affected (0.01 sec)

mysql> insert into content values('mango',2);
Query OK, 1 row affected (0.00 sec)

mysql> insert into content values('mango',3);
Query OK, 1 row affected (0.00 sec)

mysql> insert into content values('orange',4);
Query OK, 1 row affected (0.00 sec)

mysql> insert into content values('orange',5);
Query OK, 1 row affected (0.00 sec)

mysql> insert into content values('orange',6);
Query OK, 1 row affected (0.00 sec)

-- select the values:
-- ==================

mysql> select * from content;
+---------+------+
| content | sid |
+---------+------+
| apple | 1 |
| apple | 2 |
| apple | 3 |
| apple | 4 |
| apple | 5 |
| mango | 1 |
| mango | 2 |
| mango | 3 |
| orange | 4 |
| orange | 5 |
| orange | 6 |
+---------+------+
11 rows in set (0.00 sec)

mysql> select content as content,group_concat(sid) as sid,count(sid) as count from content group by content;
+---------+-----------+-------+
| content | sid | count |
+---------+-----------+-------+
| apple | 1,2,3,4,5 | 5 |
| mango | 3,2,1 | 3 |
| orange | 4,5,6 | 3 |
+---------+-----------+-------+
3 rows in set (0.00 sec)

-- Create table from another table
-- ===============================

mysql> create table new_content
-> select content as content,group_concat(sid) as sid,count(sid) as count from content group by content;
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0

-- select values
-- =============


mysql> select * from new_content;
+---------+-----------+-------+
| content | sid | count |
+---------+-----------+-------+
| apple | 1,2,3,4,5 | 5 |
| mango | 1,2,3 | 3 |
| orange | 4,5,6 | 3 |
+---------+-----------+-------+
3 rows in set (0.00 sec)


Regards,
S.Ashokkumar
Mar 27 '08 #9
yasmine
65 New Member
Hi,

Yes, we can store the values as comma separated value. Following example explain you properly and its very simple to understand... i hope...
...................

Regards,
S.Ashokkumar
Hi
Thanx 4 ur coding.
I tried it. but still i got [BLOB - 1.0 KB] in sid field (all values).
I read the description for GROUP_CONCAT() in the following link.
But i can't understand it properly...
Can u help me out........ It tells about the BLOB.

The link isGROUP_CONCAT()

Thanx n Regards
Yas.....
Mar 27 '08 #10
write2ashokkumar
39 New Member
Hi
Thanx 4 ur coding.
I tried it. but still i got [BLOB - 1.0 KB] in sid field (all values).
I read the description for GROUP_CONCAT() in the following link.
But i can't understand it properly...
Can u help me out........ It tells about the BLOB.

The link isGROUP_CONCAT()

Thanx n Regards
Yas.....

Hi,

You have mentioned the 2 tables a & b. can u able to give me the table structure of the b table with the field and datatype of the field.

Consider sid field is integer, If we group contact any integer values, we will get as string like '1,2,3,4,5'. so, we cant store the string value in the integer field. So while copy the table we must need to change the sid field value from integer to text/longtext/blob.

Regards,
S.Ashokkumar.
Mar 27 '08 #11
mwasif
802 Recognized Expert Contributor
yah....
i'm having the mysql version 4.1.11.

Thanks for the coding

This query works well and creates a table. But the survey id field does not contain comma separated values.
The sid field contains only [BLOB - 1.0 KB] values for all the rows.
I don't know what is it.
Can u help me.........????

Thanx n Regards
Yas........
Data is there, you need to use SUBSTRING() to view data or use some other GUI tool or command.
Mar 27 '08 #12
yasmine
65 New Member
Hi friends......

Thanks a lot.............. for ur valuable replies......
I fixed this by changing the sid field structure from integer to text. when i set text datatype to the sid field it automatically takes care on comma separation.


Special thanks to Ashok kumar.

I'll meet u people in threads soon with some other doubts

Thanx n regards
Yas....
Mar 28 '08 #13
write2ashokkumar
39 New Member
Hi friends......

Thanks a lot.............. for ur valuable replies......
I fixed this by changing the sid field structure from integer to text. when i set text datatype to the sid field it automatically takes care on comma separation.


Special thanks to Ashok kumar.

I'll meet u people in threads soon with some other doubts

Thanx n regards
Yas....

Hi yasmine,

i think, you are using the GUI tool to view the data from the table...
is it right? while using the some GUI tool, may be we will get the message "BLOB....". like this... so check with some other GUI tool like SQLyog, etc.. or use command line environment in windows / linux. we will get the full data.

i think, BLOB - 1.0 KB is not a mysql error... its a GUI tool error....

i hope, this will help u.

Regards,
S.Ashokkumar
Mar 28 '08 #14
yasmine
65 New Member
Hi yasmine,

i think, you are using the GUI tool to view the data from the table...
is it right? while using the some GUI tool, may be we will get the message "BLOB....". like this... so check with some other GUI tool like SQLyog, etc.. or use command line environment in windows / linux. we will get the full data.

i think, BLOB - 1.0 KB is not a mysql error... its a GUI tool error....

i hope, this will help u.

Regards,
S.Ashokkumar
Hi Ashok,
yes, exactly u r right.... am using phpMyAdmin.
Now i understood well on these things.
Thank U very much...........

Regards
Yas...
Mar 28 '08 #15
yasmine
65 New Member
The Correct code to do it is as follows:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE a (SELECT content, cast( group_concat( survey_id ) AS char ) , count( * ) count FROM b GROUP BY content ORDER BY count DESC LIMIT 30
Thanx n Regards
Yas..
Mar 31 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Craig Keightley | last post by:
is it possible to compare acomma separated list aginst another eg comma list 1 => 1,2,3,4,5 comma list 2 => 3,5 can you check that 3 is in both, and 5 is in both, therfore they match??? the...
1
by: Craig Keightley | last post by:
I can do the match perfectly but what i also need to do is create a third list of comma separated values that are in both eg: List 1 => 1,2,3,4,5,6,7,8,11 List 2 => 1,3,4,5,6,7,10,23 ...
11
by: Craig Keightley | last post by:
I have a mysql database with a list of companies who supply specific products tblSuppliers (simplified) sID | sName | goodsRefs 1 | comp name | 1,2,3,4,5 2 | company 2 | 2,4
1
by: John B. Lorenz | last post by:
I'm attempting to write an input routine that reads from a comma delimited file. I need to read in one record at a time, assign each field to a field array and then continue with my normal...
12
by: Serve Laurijssen | last post by:
Is code like the following allowed? I am talking about the comma after the last function in the initializer. void f(void) {puts("f");} void g(void) {puts("g");} struct Funcs { void...
2
by: pesso | last post by:
I have a string that contains the following: string s = "130,41,43,178,41,17,6,78,244,35,202,144,115"; They are comma separated byte numbers, and I need to initialize my byte array with them....
3
by: starman7 | last post by:
I'm attempting a query that gathers product data for a particular product id. One of the items is designer(s) which can be more than one. The product table has comma separated id's of the...
3
by: mahe23 | last post by:
All, How do One convert a comma separated column from a text file into rows in oracle. I have a scenario where the list of comma separated values changes dynamically. It is like: abc, ttt,...
17
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.