$sql = "select table2.name from table2 left join table1 on table2.id=table1.aid ";

解决方案 »

  1.   

    再做一个连接,取不一样的名字,比如$db_web_aid
      

  2.   

    补充一下:($sql要增加上一个条件是不是也是可以用上面的方法?)
    表结构:
    table1:aid,bid.....
    table2:name,id
    主要原因是MYSQL不支持嵌套只能分两次查询
    $sql="select aid from table1 where bid=5";
    $result=$DB_web->query($sql);
    while($row=$DB_web->fetch_array($result))
    {
         $aidstr=这里要得到所有的aid 串[形如(1,2,5,21)];
    }
    $sql1="select name from table2 where id in $aidstr";
      

  3.   

    连接可以用同一个,关键是怎么把ID的范围在第2次查询中根据第一次的条件缩小实际上只要实现select name from table2 where id in (select aid from table1 where bid=5)就可以了
      

  4.   

    只需内连接即可
    select * from table1,table2 where table2.id=table1.aid
      

  5.   

    也可以:$sql = "select table2.name from table2 right join table1 on table2.id=table1.aid ";
      

  6.   

    piner(piner):我后面补充说又加上了一个条件select aid from table1 where bid=5你看到了吗?想问你直接也在后面加上这个条件
    $sql = "select table2.name from table2 right join table1 on table2.id=table1.aid and table1.bid=5 ";
    可以不?