是否有可能做一个选择语句,只采取非空值?

现在我正在使用这个:

SELECT * FROM table

然后我要用php循环过滤空值。

有没有办法做到:

SELECT * (that are NOT NULL) FROM table

?

现在当我选择*时,我得到val1,val2,val3,null,val4,val5,null,null等....但我只想得到结果中不为空的值。这可能不使用循环过滤吗?


当前回答

我用\!命令从shell中grep出NULL值:

\! mysql -e "SELECT * FROM table WHERE column = 123456\G" | grep -v NULL

使用正确的.my.cnf(其中指定了数据库/用户名/密码)效果最好。这样你只需要用\!mysql e和| grep -v NULL。

其他回答

我找到了这个解决方案:

此查询为每列选择最后一个非空值。

例子


如果你有一张桌子:

id|title|body
1 |t1   |b1
2 |NULL |b2
3 |t3   |NULL

你会得到:

title|body
t3   |b2

查询


SELECT DISTINCT (

  SELECT title
  FROM test
  WHERE title IS NOT NULL 
  ORDER BY id DESC 
  LIMIT 1
) title, (

  SELECT body
  FROM test
  WHERE body IS NOT NULL 
  ORDER BY id DESC 
  LIMIT 1
) body
FROM test

希望能帮到你。

是的,在你的查询中像下面这样使用NOT NULL。

SELECT * 
FROM table
WHERE col IS NOT NULL;

Mysql不是空的连接和选择,插入,删除和逻辑操作符像或,不是

Using IS NOT NULL On Join Conditions

 SELECT * FROM users 
 LEFT JOIN posts ON post.user_id = users.id 
 WHERE user_id IS NOT NULL;

 Using IS NOT NULL With AND Logical Operator

 SELECT * FROM users 
 WHERE email_address IS NOT NULL 
 AND mobile_number IS NOT NULL;

Using IS NOT NULL With OR Logical Operator

 SELECT * FROM users 
 WHERE email_address IS NOT NULL 
 OR mobile_number IS NOT NULL;
SELECT duration,id FROM `tickets` WHERE duration !=""
SELECT * FROM TABLE_NAME
where COLUMN_NAME <> '';