Announcement

Announcement Module
Collapse
No announcement yet.

subquery returns more than one row.

Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • subquery returns more than one row.

    Hi Thankx in advance.

    I am currently working on a query that would look something like below:

    There are two tables t1 and t2 and these are the columns in each table
    t1: sid, storename
    t2. storeID, storename, keywords

    Here is the query
    $sql = mysql_query("select t1.sid from t1,t2 where (t1.sid = t2.storeID and t2.storeID = (select t2.storeID from t2 where t2.storeID like '%autos%'))");

    There error I get is subquery returns more than one results. That is eaxclty how I want it to run.. I have a query which pulls out couple of results I want to use these results to pull out other results from another table.

    If I break up the query here is how I can write
    ####################################
    METHOD2:
    $s1 = mysql_query("select t2.storeID from t2 where t2.keywords like 'auto'");
    while ($row = mysql_fetch_array($s1))
    {
    $id = $row['storeID'];
    $query_2 = mysql_quert("select t1.sid from t1 where t1.sid = '$id'");
    while ($row1 = mysql_fetch_array($query_2))
    {
    echo $row1['sid'];
    }
    }

    ###################################

    I do not want to use this method 2 where I need to write while loops. I want to know if there is a way I can write the two seperate queries as one in mysql and execute it....

    Thank you
    Charan

  • #2
    select t1.sid from t1
    left join t2 on t1.sid = t2.storeID
    where t2.keywords like '%autos%'
    group by t1.sid

    Something like that ?

    Comment

    Working...
    X