以下查询完美运行,并根据条件
插入行INSERT INTO (t1.userid,t1.name,t1.email,t1.phone) SELECT
t2.userid,t2.name,t2.email,t2.phone FROM t2 where t2.name!='';
但是,下面的查询插入0行,也不会抛出任何错误。通过使用多个where条件,查询无法正常工作
INSERT INTO (t1.userid,t1.name,t1.email,t1.phone) SELECT
t2.userid,t2.name,t2.email,t2.phone FROM t2 where (t2.name!='' AND t2.name!=0 AND
t2.name!=NULL)
答案 0 :(得分:2)
您正在错误地进行空比较并且您应该使用not null
这是一个例子
mysql> select * from test ;
+------+------+
| id | val |
+------+------+
| 1 | NULL |
| 2 | |
| 3 | 0 |
| 4 | aa |
| 5 | cc |
+------+------+
5 rows in set (0.00 sec
mysql> select * from test where val != '' and val != '0' and val != null;
Empty set (0.00 sec)
mysql> select * from test where val != '' and val != '0' and val is not null;
+------+------+
| id | val |
+------+------+
| 4 | aa |
| 5 | cc |
+------+------+
2 rows in set (0.00 sec)
mysql> select null != null ;
+--------------+
| null != null |
+--------------+
| NULL |
+--------------+
1 row in set (0.00 sec)
mysql> select null is not null ;
+------------------+
| null is not null |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
mysql> select null is null ;
+---------------+
| null is null |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
所以在你的情况下,它将是
INSERT INTO (t1.userid,t1.name,t1.email,t1.phone)
SELECT
t2.userid,t2.name,t2.email,t2.phone FROM t2 where (
t2.name!=''
AND t2.name!='0'
AND t2.name is not NULL
)
答案 1 :(得分:1)
要在mysql中检查NULL,请使用IS NULL
关键字和IS NOT NULL
所以你的条件是:=
where (t2.name!='' AND t2.name!=0 AND
t2.name IS NOT NULL);