For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > November 2004 > Search for multiple items in one query...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Search for multiple items in one query...
Bonge Boo!

2004-11-21, 8:56 pm

This is probably blazingly obvious, but my SQL is shite.

I whant to have a field in a database for realted items. I nthat field I was
going to put the ID of the related items, seperated by ,s

For example in my related items, field....

18150,18170,18190

Ideally I would then feed that string into an SQL statement and return all 3
related items.

Now I know I could do an php explode() to generate an array() from my
string, then use that array to loop through and create a "multipart" query

SELECT * FROM my_table WHERE product = 1 OR product =2 OR product =3

My possibly futile or obvious question is: can I somehow avoid having to do
the explode() and OR stuff and just feed my string into my query?


TIA

David Quinton

2004-11-22, 3:57 am

On Sun, 21 Nov 2004 22:13:06 +0000, Bonge Boo! <bingbong@spamcop.net>
wrote:

>My possibly futile or obvious question is: can I somehow avoid having to do
>the explode() and OR stuff and just feed my string into my query?


If you use more than one table, then you can use the Relational
capabilities of MySQL.

Table one would have a unique ID field - call it, say, Serial. Don't
have the "Related items" data in this table - just the other stuff.

Table two should have a Serial field that will be identical to that in
Table one etc.

<http://www.keithjbrown.co.uk/vworks.../mysql_p5.shtml>
is quite a good introduction.

--
Interesting Xmas gifts: <http://www.awin1.com/tclick.php?id=405&mid=182> . <http://BizOrg.co.uk/shopping/>
Locate your Mobile phone: <http://www.bizorg.co.uk/news.html>
Floortje

2004-11-22, 8:59 am


> Now I know I could do an php explode() to generate an array() from my
> string, then use that array to loop through and create a "multipart" query
>
> SELECT * FROM my_table WHERE product = 1 OR product =2 OR product =3


SELECT * FROM my_table WHERE product IN (1,2,3)

Floortje


Bonge Boo!

2004-11-22, 8:59 am

On 22/11/04 7:52 am, in article 41a19b56$0$78738$e4fe514c@news.xs4all.nl,
"Floortje" <floortje- apestaartjexs4allpuntnl@hetminnetjehoort
.erbij> wrote:

>
> SELECT * FROM my_table WHERE product IN (1,2,3)


You're a lovely person. I go play now!

Bonge Boo!

2004-11-22, 8:59 am

On 22/11/04 7:01 am, in article 0c33q0do6qeuif03j6h86uvvgl59kgl7ed@4ax.com,
"David Quinton" <usenet_2004D_email@REMOVETHISBITbizorg.co.uk> wrote:

> <http://www.keithjbrown.co.uk/vworks.../mysql_p5.shtml>
> is quite a good introduction.


Thank you!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com