简体   繁体   English

计算表中没有的记录数

[英]count number of records that are not in the table

I`m trying to figure out how to create sql query to DB, that will doing the next: Count the number of domains, that exists in my PHP array, but not exist in DB table. 我正在尝试弄清楚如何创建对数据库的sql查询,这将执行下一步:计算PHP数组中存在但DB表中不存在的域数。 Table structure is: 表结构为:

id|domain

I have an array with the number of domains 我有一个带有域数的数组

$domains = ("domain1", "domain2"....);

Please, help me deal with this! 请帮我解决这个问题!

Thanks!! 谢谢!!

Where's the problem? 哪里出问题了?

In pseudo-code: 用伪代码:

=> foreach entry in phpArray 
=> select entry from db 
=> if(result == false) counter++

Its general coding. 它的一般编码。 Im sure you can work out this way. 我确定您可以通过这种方式解决。

$query = "SELECT COUNT(*) FROM DOMAINS WHERE DOMAINS.DOMAIN NOT IN (" . implode(', ', $domains) . ")";

希望能帮助到你 :)

First you count your array with: 首先,使用以下方法计算数组:

$counted = count($domains);

then the query is: 那么查询是:

SELECT COUNT(domain) as counted_domains FROM table

then compare the results and you get your difference :) 然后比较结果,您会有所不同:)

Is this helpful? 这有帮助吗?

$query = "SELECT count(*) FROM table WHERE domains NOT IN ('" . join("', '", $domains) . "')";

With mysql: 使用mysql:

SELECT t1.domain
    FROM yourtable t1
    LEFT JOIN yourtable t2
        ON t1.id = t2.id
    WHERE NOT t2.domain IN ('domain1', 'domain2'...)

Select all domains from the database Store these in an array $dbdomains do something like 从数据库中选择所有域将它们存储在数组中$dbdomains做类似的事情

foreach($domains as $key->$domain) {
 if in_array($domain, $dbdomains) unset $domains[$key];
}
echo count($domains);

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

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