简体   繁体   中英

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. 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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