PDA

View Full Version : "MySQL Query" query ;-)


Ninth Dimension
08-21-2002, 02:30 PM
I've got a loop in my script that will find out a bunch of ID numbers, each of these numbers will need to be queried and the results pulled out of the database.

I could put a seperate query within each each loop to get the results, but this has two problems, first one is that it will create way too many queries on that page (up to 30 un-needed extra queries), and the second problem is that the results will be in reverse (because of the way the loop works, and the loop can't be reversed, that won't work).

what I promise to do insted is take all these ID, and run one query to get them all, and just have mysql put them into the order I want.

Problem is that I don't now what the query should look like :(

Below is an example of what I think it should look like to give you an idea of what I mean, maybe you can point me in the right direction?SELECT * FROM table WHERE id = 8,4,2,1(in this example I want it to pull id's 8, 4, 2 and 1)

Thanx

Ninth Dimension
08-21-2002, 02:31 PM
BTW, just so you know, if this is going to make it any easier (I know it will for me) the results of the loop are going to be put into an array, i.e.$array[] = 8;
$array[] = 4;
$array[] = 2;
$array[] = 1;so if it can be worked so that the query uses the array i'd really apprechate it.

Ninth Dimension
08-21-2002, 03:15 PM
I've just been talking to someone that suggested I used one of he two below, neither worked :(
"SELECT * FROM table WHERE id = 8 OR id = 4 OR id = 2 OR id = 1"
"SELECT * FROM table WHERE id = 8 AND id = 4 AND id = 2 AND id = 1"please help :bunny:

Xenon
08-21-2002, 03:20 PM
the or part is correct, be sure your table has an id column ;)

Ninth Dimension
08-21-2002, 03:25 PM
yes, it has an id column, the OR one is only returning one result.

Ninth Dimension
08-21-2002, 03:42 PM
OK, you can hit me now, i was being a pratt, it's true that I did have an id column, but it was not the id column that wanted to call.

Lets put this down to the typo LOL :)

anyway, if anyone knows how to do this via an array, i'm still interested in finding out, cheers.

TeddyBare69
08-29-2002, 11:38 AM
Have you tried this yet?

SELECT * FROM table WHERE id IN ( 8,4,2,1 )


You can also add this to put them in order

SELECT * FROM table WHERE id IN ( 8,4,2,1 ) ORDER BY id

Ninth Dimension
08-29-2002, 12:04 PM
I learnt about that a few days ago, very userful indeed. Thank you.