English 中文(简体)
表中NOT的选择性数值
原标题:Select values that are NOT in table

Let s say I have three values in PHP: "a", "b", "c". Doesn t matter whether in an array or comma separated string.
There is a table in database:

id | value  
1  | a  
2  | b  
3  | d  

My purpose is to find the values that are in php array but not in database table.
The given example will give "c".
Can I do it with only one query?

<>>>>>

在对阵列(diff)的答复中收到一些很好的建议,尽管在我看来,非行的表确实是巨大的,阵列中的项目不超过5-6个。 因此,我想最好回答5-6个问题。

最佳回答

如PHP阵列短,请设<代码>UNION ALL查询,以建立small table,然后使用NOT INLEFT JOIN。 对large的查询(以更快的速度) 表:

SELECT value
FROM (
    SELECT  a  AS value
    UNION ALL
    SELECT  b 
    UNION ALL
    SELECT  c 
) AS php_array_values
WHERE value NOT IN (
    SELECT value
    FROM that_large_table
);

或者,您可在临时表格中插入“ php”阵列值,并使用<条码>或<条码>>查询。 当然,这意味着你最后提出三个额外的询问:

CREATE TEMPORARY TABLE IF NOT EXISTS php_array_values (value VARCHAR(100));

DELETE FROM php_array_values;

INSERT INTO php_array_values VALUES ( a ), ( b ), ( c );

SELECT php_array_values.value
FROM php_array_values
LEFT JOIN that_large_table ON php_array_values.value = that_large_table.value
WHERE that_large_table.value IS NULL
问题回答

如何做到这一点?

<?php
    $a = array( a ,  b ,  c );
    $values = implode(" , ", $a);
    $sql = "SELECT DISTINCT `value` FROM `mytable` WHERE `value` IN ( $values )";
    echo $sql;

进行 s。 结果将是你已经掌握的0至3个要素。 接下来是一阵列(这根本不会是沉重的),因为你有最初的小阵列,而布邦的阵列则更少。

$not_in_db = array_diff($a, $array_from_sql_result);

如果你用 com子分离的价值观加以扼杀,那么你首先需要“开发”:

$s = "a,b,c";
$a = explode(",", $s);

您可以选择表格中的所有条目,然后填写array_diff(

但这只是一个问题,是一个问题,有些是员额处理。

为此,我将把数值从表格中推入一个阵列,并使用array_diff

REF:

Instead of pulling all the elements from the DB you can try using LIKE statement, this will reduce the number of entries pulled from DB.

例如:

PHP阵列价值:a,b,c

亚洲开发银行的价值观:a,b,d

select value from your_table_name where (value LIKE  %a% ) OR (value LIKE  %b% ) OR (value LIKE  %c% );

页: 1

现在使用array_diff php。

select max(*) as count from tablename 
where field1 != $arr[0] && field2 != $arr[1] && field3 != $arr[2];

如果你愿意,你可以使用或操作。 如果这一询问的回报为0,那么数据库中已不存在阵列价值。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签