将数据从一个表复制到另一个表,但添加第二个WHERE子句不会插入数据

时间:2016-06-24 17:58:54

标签: php mysql where-clause

我有两张桌子。当用户单击某个按钮时,它会将表1中的数据复制到表2中,表2中已存在该数据。

我的查询工作得很好,直到我在名为" onsite"的第一个表中添加了一个新列。 - 设置为yes,或者为NULL。

以下是我尝试的查询。它不再将任何数据插入past_bidder表(表2)

$copybidderquery = "INSERT INTO past_bidders(bidnum, bidfname, bidlname, bidphnum, bidlicense, bidaddress, bidtaxexempt, bidtaxid, date_created) 
SELECT bidnum, bidfname, bidlname, bidphnum, bidlicense, bidaddress, bidtaxexempt, bidtaxid, date_created 
FROM bidders 
WHERE (bidfname, bidlname, bidphnum, bidlicense, bidaddress) 
NOT IN (SELECT bidfname, bidlname, bidphnum, bidlicense, bidaddress FROM past_bidders) 
AND onsite != 'yes'";

我也尝试过移动现场!='是'在#34; WHERE"之后它仍然做同样的事情。

$copybidderquery = "INSERT INTO past_bidders(bidnum, bidfname, bidlname, bidphnum, bidlicense, bidaddress, bidtaxexempt, bidtaxid, date_created) 
SELECT bidnum, bidfname, bidlname, bidphnum, bidlicense, bidaddress, bidtaxexempt, bidtaxid, date_created 
FROM bidders 
WHERE onsite != 'yes'
AND (bidfname, bidlname, bidphnum, bidlicense, bidaddress) 
NOT IN (SELECT bidfname, bidlname, bidphnum, bidlicense, bidaddress FROM past_bidders)";

如前所述,此查询在没有" onsite的情况下工作正常!='是'"线。

我认为这只是一些我忽略的语法错误。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

INSERT INTO past_bidders(bidnum, bidfname, bidlname, bidphnum, bidlicense, bidaddress, bidtaxexempt, bidtaxid, date_created) 
SELECT bidnum, bidfname, bidlname, bidphnum, bidlicense, bidaddress, bidtaxexempt, bidtaxid, date_created 
FROM bidders 
WHERE  onsite is NULL AND (bidfname, bidlname, bidphnum, bidlicense, bidaddress) 
NOT IN (SELECT bidfname, bidlname, bidphnum, bidlicense, bidaddress FROM past_bidders) 
;

当你说不等于它不会返回空值时,请检查以下示例

mysql> select * from calls;
+----+------------+---------+
| id | date       | user_id |
+----+------------+---------+
|  1 | 2016-06-22 |       1 |
|  2 | 2016-06-22 |    NULL |
|  3 | 2016-06-22 |    NULL |
|  4 | 2016-06-23 |       2 |
|  5 | 2016-06-23 |       1 |
|  6 | 2016-06-23 |       1 |
|  7 | 2016-06-23 |    NULL |
+----+------------+---------+
7 rows in set (0.00 sec)

mysql> select * from calls where user_id!=1;
+----+------------+---------+
| id | date       | user_id |
+----+------------+---------+
|  4 | 2016-06-23 |       2 |
+----+------------+---------+
1 row in set (0.00 sec)

mysql> select * from calls where user_id is null;
+----+------------+---------+
| id | date       | user_id |
+----+------------+---------+
|  2 | 2016-06-22 |    NULL |
|  3 | 2016-06-22 |    NULL |
|  7 | 2016-06-23 |    NULL |
+----+------------+---------+
3 rows in set (0.00 sec)

答案 1 :(得分:1)

可能有隐藏的字符或值不是'是'或者你应该检查不是空

$copybidderquery = "INSERT INTO past_bidders(bidnum, bidfname, bidlname, bidphnum, bidlicense, bidaddress, bidtaxexempt, bidtaxid, date_created) 
                                  SELECT bidnum, bidfname, bidlname, bidphnum, bidlicense, bidaddress, bidtaxexempt, bidtaxid, date_created 
                FROM bidders 
                WHERE (bidfname, bidlname, bidphnum, bidlicense, bidaddress) 
                NOT IN (SELECT bidfname, bidlname, bidphnum, bidlicense, bidaddress FROM past_bidders) 
                AND trim(lower(onsite) )!= 'yes';

答案 2 :(得分:1)

当值为!=时,

NULL会返回NULL。你有两个选择:

SELECT bidnum, bidfname, bidlname, bidphnum, bidlicense, bidaddress, bidtaxexempt, bidtaxid, date_created 
FROM bidders 
WHERE (bidfname, bidlname, bidphnum, bidlicense, bidaddress) NOT IN
          (SELECT bidfname, bidlname, bidphnum, bidlicense, bidaddress
           FROM past_bidders
          ) AND
      onsite is null;

或者,使用NULL安全平等:

SELECT bidnum, bidfname, bidlname, bidphnum, bidlicense, bidaddress, bidtaxexempt, bidtaxid, date_created 
FROM bidders 
WHERE (bidfname, bidlname, bidphnum, bidlicense, bidaddress) NOT IN
          (SELECT bidfname, bidlname, bidphnum, bidlicense, bidaddress
           FROM past_bidders
          ) AND
      (NOT onsite <=> 'yes');