MySQL 只选择非空值

发布于 2024-10-22 01:50:28 字数 333 浏览 4 评论 0原文

是否可以执行仅采用 NOT NULL 值的 select 语句?

现在我正在使用这个:

SELECT * FROM table

然后我必须使用 php 循环过滤掉空值。

有没有办法做到:

SELECT * (that are NOT NULL) FROM table

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

Is it possible to do a select statement that takes only NOT NULL values?

Right now I am using this:

SELECT * FROM table

And then I have to filter out the null values with a php loop.

Is there a way to do:

SELECT * (that are NOT NULL) FROM table

?

Right now when I select * I get val1,val2,val3,null,val4,val5,null,null etc.... but I just want to get the values that are not null in my result. Is this possible without filtering with a loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

嘦怹 2024-10-29 01:50:28

您应该使用IS NOT NULL。 (比较运算符 =<> 都在表达式两侧给出 UNKNOWNNULL。 )

SELECT * 
FROM table 
WHERE YourColumn IS NOT NULL;

为了完整起见,我会提到在 MySQL 中您还可以否定 null 安全相等运算符 但这不是标准 SQL。

SELECT *
FROM table 
WHERE NOT (YourColumn <=> NULL);

编辑以反映评论。听起来您的表可能不是第一范式,在这种情况下更改结构可能会使您的任务更容易。不过还有其他几种方法...

SELECT val1 AS val
FROM  your_table
WHERE val1 IS NOT NULL
UNION ALL
SELECT val2 
FROM  your_table
WHERE val2 IS NOT NULL
/*And so on for all your columns*/

上述方法的缺点是它会为每列多次扫描表一次。下面可能可以避免这种情况,但我还没有在 MySQL 中测试过这一点。

SELECT CASE idx
         WHEN 1 THEN val1
         WHEN 2 THEN val2
       END AS val
FROM   your_table
        /*CROSS JOIN*/
       JOIN (SELECT 1 AS idx
                   UNION ALL
                   SELECT 2) t
HAVING val IS NOT NULL  /*Can reference alias in Having in MySQL*/

You should use IS NOT NULL. (The comparison operators = and <> both give UNKNOWN with NULL on either side of the expression.)

SELECT * 
FROM table 
WHERE YourColumn IS NOT NULL;

Just for completeness I'll mention that in MySQL you can also negate the null safe equality operator but this is not standard SQL.

SELECT *
FROM table 
WHERE NOT (YourColumn <=> NULL);

Edited to reflect comments. It sounds like your table may not be in first normal form in which case changing the structure may make your task easier. A couple of other ways of doing it though...

SELECT val1 AS val
FROM  your_table
WHERE val1 IS NOT NULL
UNION ALL
SELECT val2 
FROM  your_table
WHERE val2 IS NOT NULL
/*And so on for all your columns*/

The disadvantage of the above is that it scans the table multiple times once for each column. That may possibly be avoided by the below but I haven't tested this in MySQL.

SELECT CASE idx
         WHEN 1 THEN val1
         WHEN 2 THEN val2
       END AS val
FROM   your_table
        /*CROSS JOIN*/
       JOIN (SELECT 1 AS idx
                   UNION ALL
                   SELECT 2) t
HAVING val IS NOT NULL  /*Can reference alias in Having in MySQL*/
傲性难收 2024-10-29 01:50:28

您可以过滤掉特定列中包含 NULL 值的行:

SELECT col1, col2, ..., coln
FROM yourtable
WHERE somecolumn IS NOT NULL

如果您想过滤掉任何列中包含 NULL 值的行,请尝试以下操作:

SELECT col1, col2, ..., coln
FROM yourtable
WHERE col1 IS NOT NULL
AND col2 IS NOT NULL
-- ...
AND coln IS NOT NULL

更新:根据您的评论,也许您想要这个?

SELECT * FROM
(
    SELECT col1 AS col FROM yourtable
    UNION
    SELECT col2 AS col FROM yourtable
    UNION
    -- ...
    UNION
    SELECT coln AS col FROM yourtable
) T1
WHERE col IS NOT NULL

我同意马丁的观点,如果你需要这样做,那么你可能应该改变你的数据库设计。

You can filter out rows that contain a NULL value in a specific column:

SELECT col1, col2, ..., coln
FROM yourtable
WHERE somecolumn IS NOT NULL

If you want to filter out rows that contain a null in any column then try this:

SELECT col1, col2, ..., coln
FROM yourtable
WHERE col1 IS NOT NULL
AND col2 IS NOT NULL
-- ...
AND coln IS NOT NULL

Update: Based on your comments, perhaps you want this?

SELECT * FROM
(
    SELECT col1 AS col FROM yourtable
    UNION
    SELECT col2 AS col FROM yourtable
    UNION
    -- ...
    UNION
    SELECT coln AS col FROM yourtable
) T1
WHERE col IS NOT NULL

And I agre with Martin that if you need to do this then you should probably change your database design.

夜深人未静 2024-10-29 01:50:28
Select * from your_table 
WHERE col1 and col2 and col3 and col4 and col5 IS NOT NULL;

这种方法的唯一缺点是只能比较 5 列,之后结果将始终为 false,因此我只比较可以为 NULL 的字段。

Select * from your_table 
WHERE col1 and col2 and col3 and col4 and col5 IS NOT NULL;

The only disadvantage of this approach is that you can only compare 5 columns, after that the result will always be false, so I do compare only the fields that can be NULL.

回首观望 2024-10-29 01:50:28

我找到了这个解决方案:

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

示例


如果您有一个表:

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

我希望对您有所帮助。

I found this solution:

This query select last not null value for each column.

Example


If you have a table:

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

you get:

title|body
t3   |b2

Query


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

I hope help you.

笛声青案梦长安 2024-10-29 01:50:28

以下查询对我有用,

当我设置了列“NULL”的默认值时,

select * from table where column IS NOT NULL

当我设置了默认值时,什么也没有

select * from table where column <>''

Following query working for me

when i have set default value of column 'NULL' then

select * from table where column IS NOT NULL

and when i have set default value nothing then

select * from table where column <>''
愿得七秒忆 2024-10-29 01:50:28

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

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

它与指定数据库/用户名/密码的正确 .my.cnf 配合使用效果最佳。这样你只需要用 \ 包围你的 select 即可! mysql e| grep -v NULL

I use the \! command within MySQL to grep out NULL values from the shell:

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

It works best with a proper .my.cnf where your database/username/password are specified. That way you just have to surround your select with \! mysql e and | grep -v NULL.

兰花执着 2024-10-29 01:50:28

是的,在查询中使用 NOT NULL ,如下所示。

SELECT * 
FROM table
WHERE col IS NOT NULL;

Yes use NOT NULL in your query like this below.

SELECT * 
FROM table
WHERE col IS NOT NULL;
所谓喜欢 2024-10-29 01:50:28

您甚至不必使用星号来获取表字段。

You didn't even have to use an asterisk to get the table fields.

永言不败 2024-10-29 01:50:28

为 int 列添加条件 >0,为 varchar 列添加 != ""

select column1_id, column2, colummn3, where column1_id>0 and column2 != ""

Add condition >0 for int columns and != "" for varchar columns

select column1_id, column2, colummn3, where column1_id>0 and column2 != ""
浪漫之都 2024-10-29 01:50:28
SELECT duration,id FROM `tickets` WHERE duration !=""
SELECT duration,id FROM `tickets` WHERE duration !=""
怀里藏娇 2024-10-29 01:50:28

WHERE COALESCE(您的所有列)不为空

WHERE COALESCE(ALL YOUR COLUMNS) IS NOT NULL

爱,才寂寞 2024-10-29 01:50:28
SELECT * FROM TABLE_NAME
where COLUMN_NAME <> '';
SELECT * FROM TABLE_NAME
where COLUMN_NAME <> '';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文