mysql有和where子句在同一个查询上

时间:2012-07-02 00:38:04

标签: php mysql sql select

这是我的代码:

$sqlz = "SELECT t1.user_id, t2.status, t2.email 
         FROM coverages t1 
         LEFT JOIN users t2 ON t1.user_id = t2.user_id 
         GROUP BY t1.user_id  
         HAVING COUNT(t1.user_id) =".$value;

我想添加“WHERE users.email IS NOT NULL” 当我添加它时,它返回一个白页/没有结果。据我所知,db上至少有200个结果,其中包含一封电子邮件,并且符合该条件。

这是我所做的不起作用的一个例子:

 $sqlz =    "SELECT t1.user_id, t2.status, t2.email 
             FROM coverages t1 
             LEFT JOIN users t2 ON t1.user_id = t2.user_id
             WHERE users.email IS NOT NULL 
             GROUP BY t1.user_id  
             HAVING COUNT(t1.user_id) =".$value;

1 个答案:

答案 0 :(得分:15)

我认为您需要使用t2别名)代替users

 $sqlz =    "SELECT t1.user_id, t2.status, t2.email 
             FROM coverages t1 
                     LEFT JOIN users t2 ON t1.user_id = t2.user_id
             WHERE t2.email IS NOT NULL 
             GROUP BY t1.user_id  
             HAVING COUNT(t1.user_id) = " .$value;