SQL Query在另一个表上使用WHERE子句从表中进行选择

时间:2014-09-15 06:43:57

标签: php mysql sql

我正在尝试在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

1 个答案:

答案 0 :(得分: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早已弃用