Bluehost.com Web Hosting $6.95

select multiple values for column in one query

This is a discussion on select multiple values for column in one query within the MySQL Database forums, part of the Database Forums category; I have a table with a column that contains a comma seperated list of ids as TEXT that are used ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-25-2007
techcalgary@hotmail.com
 
Posts: n/a
Default select multiple values for column in one query

I have a table with a column that contains a comma seperated list of
ids as TEXT that are used to reference multiple rows in another table.
so "2,4" would reference id 2 and 4 in table2.

select * from table2 where id = 3 or id = 4;

Is there a way to do that without going through the list and
delimiting with " or id = "? Something like

select * from table2 where id in values(3,4);

I thought I'd seen something like that before but I can't find it in
the mysql manual.

Is using a TEXT for an id list bad structure? Is there a better way to
store an array of ids in a column in mysql?

Reply With Quote
  #2 (permalink)  
Old 03-25-2007
techcalgary@hotmail.com
 
Posts: n/a
Default Re: select multiple values for column in one query

I guess I could do

select * from table2 order by field( id, 3, 4 ) desc limit 2;

but isn't there a way to do that in the where clause?

Reply With Quote
  #3 (permalink)  
Old 03-25-2007
Good Man
 
Posts: n/a
Default Re: select multiple values for column in one query

techcalgary@hotmail.com wrote in news:1174793194.111464.20170
@o5g2000hsb.googlegroups.com:

> I have a table with a column that contains a comma seperated list of
> ids as TEXT that are used to reference multiple rows in another table.
> so "2,4" would reference id 2 and 4 in table2.
>
> select * from table2 where id = 3 or id = 4;
>
> Is there a way to do that without going through the list and
> delimiting with " or id = "? Something like
>
> select * from table2 where id in values(3,4);
>
> I thought I'd seen something like that before but I can't find it in
> the mysql manual.
>


Without answering the main question, and trying to be helpful, can I say:

a) When you want to store an array of IDs in a table, it usually means you could
use better table design, and most often, adding a table that has a 'many-to-
many' type of relationship. In your example, I'm not quite sure what your first
selection would be, but let's say you're looking for "basketball", and you want
your query to return information about table2's id '3' and id '4'. Instead of
having 'basketball' as one column in your first table and '3,4' in the column
next to it, you'd JUST have 'basketball' in your table, with it's ID (1). Then,
in your new many-to-many table, you'd have one column referencing the first
table's ID (say, "table1ID") and a column for the tableID in table 2 (say,
"table2ID"). So, in your case, in this new many-to-many table, there are TWO
entries: i) '1' in the first column and '3' in the second and ii) '1' in the
first column and '4' in the second. Just by looking at this new table, you can
see that entry 1 in your first table('basketball') is related to ID 3 and ID 4
from the second table. A query that would return this information (untested,
example only) is:

SELECT table2.* FROM table1
JOIN newtable ON table1.ID=newtable.table1ID
JOIN table2 ON newtable.table2ID=table2.ID
WHERE table1.ID=1


> Is using a TEXT for an id list bad structure? Is there a better way to
> store an array of ids in a column in mysql?


Storing text as a primary key (which I think is your question?) is a bad idea.
Numbers are fastest, then VARCHARS i suppose, but certainly TEXT/BLOBs should be
avoided.



Reply With Quote
  #4 (permalink)  
Old 03-25-2007
Jerry Stuckle
 
Posts: n/a
Default Re: select multiple values for column in one query

techcalgary@hotmail.com wrote:
> I have a table with a column that contains a comma seperated list of
> ids as TEXT that are used to reference multiple rows in another table.
> so "2,4" would reference id 2 and 4 in table2.
>
> select * from table2 where id = 3 or id = 4;
>
> Is there a way to do that without going through the list and
> delimiting with " or id = "? Something like
>
> select * from table2 where id in values(3,4);
>
> I thought I'd seen something like that before but I can't find it in
> the mysql manual.
>
> Is using a TEXT for an id list bad structure? Is there a better way to
> store an array of ids in a column in mysql?
>


Yes, this is a poor design. Google for "database normalization" for a
lot of good information.

When you have a relationship, you're generally better off creating a
third table with two columns - one for each id associated in the
relationship. Much more flexible.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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




All times are GMT +1. The time now is 07:30 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0