Debt Consolidation - VeraciTek, Inc. - We Build Websites - Advertising - Wordpress Themes - Find jobs

PDA

View Full Version : Is it possible to have a multiple select in one query?


123GoToAndPlay
Mar 18th 2006, 11:07 am
Hi

Is it possible to have a multiple select in one query like

SELECT * FROM images WHERE id = 8, 10, 4


this ofcourse doesn't work

Gaffer Sports
Mar 18th 2006, 12:21 pm
Hi

Is it possible to have a multiple select in one query like

SELECT * FROM images WHERE id = 8, 10, 4


this ofcourse doesn't work


SELECT * FROM images WHERE id = 8 OR id = 10 OR id = 4


This should work for you.

Steve

123GoToAndPlay
Mar 18th 2006, 3:06 pm
@gaffersports,

well I found this one

SELECT * FROM images WHERE id IN(8,10,4)


Never used IN before so this will be the first time ;)

Now I am stuck with the php part

$sql = "SELECT * FROM images WHERE id IN (";
foreach($ids_today as $id_today )
{
$sql .= ",$id_today";
}
$sql .= ")";


gives me

SELECT * FROM images WHERE id IN(,8,10,4)


need to get rid of the ,

certainly going to try your suggestions as well.

123GoToAndPlay
Mar 18th 2006, 3:27 pm
ok with implode I have it up and running

$tmp_ids = array();
$sql = "SELECT * FROM images WHERE id IN (";
foreach($ids_today as $id_today )
{
$tmps_ids[] = $id_today;
}
$sql .= implode(",", $tmps_ids);
$sql .= ")";

echo $sql;


Not very elegant but that will be next I guess.

123GoToAndPlay
Mar 18th 2006, 3:34 pm
oh rewritten it to

$tmp_ids = array();
foreach($ids_today as $id_today )
{
$tmps_ids[] = $id_today;
}

$sql = "SELECT * FROM images WHERE id IN (".implode(",", $tmps_ids).")";

onlywin
May 21st 2009, 12:39 pm
yes, I paste your version and a version that works:

your version: SELECT * FROM images WHERE id = 8, 10, 4

correct: SELECT * FROM images WHERE id = 8 OR id = 10 OR id= 4

luizeba
May 24th 2009, 3:20 pm
Are you sure it works, onlywin?

mwasif
May 25th 2009, 12:28 pm
This is quite an old thread. Didn't you notice that the poster already posted the solution?