473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Create list from comma separated fields

I need to create an alphabetized list from multiple rows that contain comma separated tags.

Example:

Row1 -> Lessons, Discussions, Help
Row2 -> Discussions, Chat
Row3 -> Surveys, Evaluations, Discussions

Result needs to be:
Chat, Discussions, Evaluations, Help, Lessons, Surveys

Note that repeated items need to only appear once in the final alphabetized list. I have no idea how to go about making a query that will do this. Please help! :)
Mar 24 '10 #1
1 4175
Atli
5,058 Expert 4TB
Hey.

That is not doable in any *practical* way. - The first rule of relational database design (the first normal form; 1NF) states that each field should only ever contain a single value. Putting a comma-separated list of values into single field violates that. - That rule exists precisely to prevent situations like these; having to manually manipulate a field to extract several values.

To put it simply; MySQL only recognizes each field as a single value. Because you have multiple values inside a single field, MySQL considers all of them to be a single value, and can therefore not alphabetize them correctly. - You will have to manually extract the values from the field, separate them, and then alphabetize them.

Your best option would be to correct the database design. Extract the multiple values from the fields, separate them, put them in a new table and link them to the rows they belong to by a foreign key. Then you can create a simple MySQL query that fetches them and returns them as you want them to be.

The tables should look something like:
Expand|Select|Wrap|Line Numbers
  1. +---------+     +------------+
  2. | stuff   |     | tags       |
  3. +---------+     +------------+
  4. | id (PK) |>--  | id (PK)    | 
  5. | etc...  |  |  | value      |
  6. +---------+  -->| owner (FK) |
  7.                 +------------+
From which you could get a list of tags by just querying the "tags" table.

And if you need to fetch a comma-separated list of tags with your rows from "stuff", you can generate that by using the GROUP_CONCAT function:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     `stuff`.`id`
  3.     GROUP_CONCAT(DISTINCT `tags`.`value`) as 'tags'
  4. FROM    `stuff`
  5. LEFT JOIN `tags`
  6.     ON `tags`.`owner` = `stuff`.`id`
  7. GROUP BY `stuff`.`id`
Your alternative, if you want to stick to your current database structure, is to create a complex query that somehow separates the fields and sorts them (which would no doubt be highly inefficient), create a procedure that programmatically does the same, or just pull the data to your front-end and do it there.
Mar 24 '10 #2

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: Not Me | last post by:
Hi, I'm sure this is a common problem.. to create a single field from a whole column, where each row would be separated by a comma. I can do this for a specified table, and column.. and I've...
0
by: amazononthemoon | last post by:
I have not been able to find any examples of the type of validation I need. I have numeric fields on a web page that I only want them to enter numbers with two decimal places. I would like to...
1
by: DCM Fan | last post by:
Access 2K, SP3 on Windows 2K, SP4 All, I have an import spec set up with quoted Identifiers and comma-separated values. The text file is produced by a 3rd-party program of which I have no...
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...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
10
by: Nishanth | last post by:
I want to know how to validate a string which is comma separated "C1","2","12344","Mr","John","Chan","05/07/1976"......... I need to validate each field value against a set of rules, for...
5
by: Jetus | last post by:
I have a comma delimited file that is separated by comma's, and then sometimes by "," c:\temp\05-06-08\Sale1,659 CECIL,"659 CECIL,40211", 1,659,CECIL,AVENUE,LOUISVILLE,40211,"$65,276.78 "...
5
by: shapper | last post by:
Hello, I have a Linq query which returns items of List<TagTags = new List<Tag(from t in database.Tags where t.Category = MyCategory).ToList(); Each tag has three properties: ID, Name and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.