简体   繁体   English

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

[英]MYSQL Count from two tables if two conditions match

I have two mysql tables on script. 我在脚本上有两个mysql表。 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. 现在,我需要显示一些通知,但问题是为此使用了两个不同的表,其中一个显示给用户(如果有待处理),第二个显示给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'而第二个为='1'则得到计数?

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM