douzang7928 2016-02-22 10:14 采纳率: 100%
浏览 55
已采纳

如果两个条件匹配,MYSQL从两个表计数

I have two mysql tables on script. Now I need to display some notifications but problem is that two different tables are used for that and in one it's showed for user if there is pending and in second for admin. So admin must approve it for user to be showed.

This is my code:

$test=$mysql->echo_one("
    SELECT COUNT(*)
    FROM `cpm_ad_mapping`
    WHERE
        `status` = '-1' 
    AND `uid`    = '$uid'
");
$thead->setValue("{NON}", $test);

$test2=$mysql->echo_one("
    SELECT COUNT(*)
    FROM `cpm_ads`
    WHERE
        `status` = '1' 
    AND `uid`    = '$uid'
");
$thead->setValue("{NON1}", $test2);

How can I match these two status values so I get count if first is ='-1' and second is ='1'?

  • 写回答

1条回答 默认 最新

  • dss89001 2016-02-22 10:25
    关注

    What you need is a join between this two tables:

    SELECT COUNT(*)
    FROM `cpm_ad_mapping` s
    INNER JOIN `cpm_ads` t ON s.uid = t.uid
    WHERE s.status = '-1' 
         AND t.status = '1'
         AND s.uid = '$uid'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?