3

I'm trying to filter a table to SELECT the values to certain rows based on condition. The first rather complex SELECT works fine yielding me a group of id's. But I want to use this id group ($art_id) in an array for the 2nd SELECT to get rows more straight forwardly. Hope someone ha suggestions. Thanks, Allen.

the first SELECT is not included here -- I do get values with print $art_id; ...but only the last value shows up in my image list SELECT because I need to use an array properly

$QUERY1="SELECT.....etc,..."   ///this works fine

$res = mysql_query($QUERY1);
   $num = mysql_num_rows($res);
   if($num>0){
   while($row = mysql_fetch_array($res)){
       $art_id = $row['art_id'];

print $art_id;
$a1 = array($art_id); ///this $a1 var didn't work in the SELECT below.
   }
}
///here is where I need to have an array var instead of just $art_id
$QUERY2="SELECT * FROM artWork WHERE art_id = '$art_id'";  

    $res = mysql_query($QUERY2);
   $num = mysql_num_rows($res);
   if($num>0){

   while($row = mysql_fetch_array($res)){
       $art_title = $row['art_title'];
       $artist_name = $row['artist_name'];
       $art_id = $row['art_id'];
                 $media = $row['media'];

  echo.....etc,...../// only one image (the last, of course) shows up here
3
  • Sounds like a simple IN() clause should do the trick in 1 query? Commented Oct 15, 2010 at 23:49
  • Just a small note. IN is an operator. Not to be confused with a clause or a function. Commented Oct 15, 2010 at 23:58
  • As golden rule, include as much information as you can (I'm referring to the first query) Commented Oct 16, 2010 at 0:27

3 Answers 3

3

you can resolve this problem by using just query if you like:

let's say that you first query get the art_id from the table table1 so:

SELECT art_id FROM table1

and you next query it's:

SELECT * FROM artWork WHERE art_id = '$art_id'

The final query obtained by merging the other 2 queries:

SELECT * FROM artWork WHERE art_id in ( SELECT art_id FROM table1 );
Sign up to request clarification or add additional context in comments.

Comments

0

Just use this to do it in one query.

SELECT [...] WHERE `art_id` IN (SELECT `art_id` [...])

Although this is redundant if you select from the same table in both queries.

If you want to test for values from a PHP array you should use IN like this:

$values = array(1,2,3);
'WHERE `art_id` IN ('.implode(',', $values).')'

2 Comments

Thanks, Alin. Worked great. With 70 vars compared in different groupings (e.g., paintings not the same media as sculpture or photos) it was complex to figure out. But you pointed me in the right direction with query within query. -much appreciated, Allen
No problem. Also note that on stackoverflow it is customary to accept the answers you consider best answer your questions.
0

Use a join, it's pretty simple and much faster.

SELECT aw.* FROM table1 t
JOIN artWork aw ON aw.art_id=t.art_id
WHERE (CONDITIONS);

where CONDITIONS is the conditions from your first query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.