SQL查询,使用另一个表上的WHERE子句从表中进行选择


SQL Query to select from a table using a WHERE clause on another table

我正在尝试在PHP:中运行此SQL查询

$sql="select b.company as c, c.company, c.resellerid from billing b, customer c WHERE (b.company = c.customerid OR b.company = c.voip_account OR b.company = c.sequence) AND c.resellerid = '0' and source = 'CDR' group by b.company asc ";
$rs=mysql_query($sql,$conn);
while($result=mysql_fetch_array($rs)) {
    $sql2="select company,extension,phone,calltype,seconds,who,customer_bill,inclusive,timestamp from billing where company='".$result["c"]."' and source='CDR' ";
    $rs2=mysql_query($sql,$conn);
    $result2=mysql_fetch_array($rs2);
    echo $result["c"].' - '.$result2["company"].'<br>';
}

billing.company可以等于以下中的任何一个

customer.sequence
customer.customerid
customer.voip_account

我想选择billing表中customer.resellerid = 0和:所在的所有行

billing.company = customer.sequence OR billing.company = customer.customerid OR billing.company = customer.voip_account

但其返回的行resellerid不等于0

我不明白为什么有两个查询。。。

SELECT DISTINCT b.company b_company
              , b.extension
              , b.phone
              , b.calltype
              , b.seconds
              , b.who
              , b.customer_bill
              , b.inclusive,timestamp
              , c.company c_company
              , c.resellerid 
           FROM billing b
           JOIN customer c 
             ON b.company IN(c.company,c.voip_account,c.sequence) 
            AND c.resellerid = 0 
            AND source = 'CDR' 
          ORDER 
             BY b.company;

请注意,PHP的mysql_ API早就不推荐使用了