24 messages in com.mysql.lists.mysqlRe: how to deal with a string of cate...
FromSent OnAttachments
J S23 Jul 2004 13:20 
Aman Raheja23 Jul 2004 14:16 
J S23 Jul 2004 15:18 
J S24 Jul 2004 14:47 
leegold24 Jul 2004 16:42 
Michael Stassen24 Jul 2004 17:17 
leegold24 Jul 2004 19:09 
Michael Stassen24 Jul 2004 22:14 
J S26 Jul 2004 04:54 
Mojtaba Faridzad26 Jul 2004 07:55 
Martijn Tonies26 Jul 2004 08:05 
Brent Baisley26 Jul 2004 08:09 
SGr...@unimin.com26 Jul 2004 08:15 
Mojtaba Faridzad26 Jul 2004 08:34 
J S26 Jul 2004 08:35 
SGr...@unimin.com26 Jul 2004 08:50 
Mojtaba Faridzad27 Jul 2004 04:51 
Wolfgang Riedel27 Jul 2004 05:25 
Victor Pendleton27 Jul 2004 05:33 
Mojtaba Faridzad27 Jul 2004 05:58 
Wolfgang Riedel27 Jul 2004 07:41 
J S27 Jul 2004 11:57 
Leonardo Javier Belén27 Jul 2004 13:03 
Leonardo Javier Belén28 Jul 2004 07:14 
Subject:Re: how to deal with a string of categories
From:SGr...@unimin.com (SGr@unimin.com)
Date:07/26/2004 08:50:12 AM
List:com.mysql.lists.mysql

http://dev.mysql.com/doc/mysql/en/GROUP-BY-Functions.html

Is where you will find the GROUP_CONCAT function in the manual.

Yours, Shawn Green Database Administrator Unimin Corporation - Spruce Pine

"Mojtaba Faridzad" <mfar@pylonelectronics.com> wrote on 07/26/2004 11:34:30 AM:

Thank Martijn, Brent, and Shawn so much! I did not know about GROUP_CONCAT() funtion. I checked MySQL document page but just under "string function" in User Comments section there was an example of using it. Shawn, where can I find the syntax of this command? it looks like we can pass some parameters to it too (like SEPARATOR)

thanks, Mojtaba

----- Original Message ----- From: SGr@unimin.com To: Mojtaba Faridzad Cc: mys@lists.mysql.com Sent: Monday, July 26, 2004 11:15 AM Subject: Re: how to deal with a string of categories

May I suggest a design change as a solution? I would suggest that you should create a user/category table to replace your "catstr" field:

CREATE TABLE user_category ( user_Id int not null, category_Code char(1) not null UNIQUE (user_Id, category_Code) )

There will be one record in the user_category table for each category that a user belongs to.

To see a list of all of your users and to which cateogories each user belongs:

SELECT u.user_Name, category_Name FROM user u INNER JOIN user_category uc ON uc.user_ID = u.user_id INNER JOIN category cat ON cat.category_Code = uc.category_Code

or if you wanted a comma-separated list of categories for each person:

SELECT u.user_Name, GROUP_CONCAT(category_Name) as categories FROM user u INNER JOIN user_category uc ON uc.user_ID = u.user_id INNER JOIN category cat ON cat.category_Code = uc.category_Code GROUP BY u.user_Name

NOTE: you will have to change the query examples I gave you to match your actual table and field names!!!!

This does not limit you to having only 10 (or 20 or 30) categories for each person. It also means that creating new categories will not require a change in your database design (changing the size of a column) but only adding or deleting records.

Yours,

Shawn Green Database Administrator Unimin Corporation - Spruce Pine

"Mojtaba Faridzad" <mfar@pylonelectronics.com> wrote on 07/26/2004 10:55:22 AM:

Hi,

I'd like to know how you guys write SQL command for this problem. there are some categories which I give them "A", "B", ... and I have a string field (catstr) with 10 characters in "mytable" to keep selected categories in a record. when user selects "C", "G", "K", I keep "CGK" in "catstr" field. categories has been defined in "cattable". now I want to write a query to retreive these catergories. query will have 10 columns ("catstr" is 10 characters) with description of categories. it means user doesn't see "C", or "G".

I tried to open 10 times "cattable" with different alias and make the query but it looks like MySQL doesn't like it and doesn't let me open the same table more than once (even with different alias). I can create 10 temporary tables and solve this problem and it doesn't look good. or I can create my query with 10 CASE commands with I create them base on "cattable". how you guys solve this problem? maybe there is a better solution which I don't know and very neat can solve this problem.

thanks, Mojtaba