【问题标题】:how to select both true and false column value in mysql如何在mysql中同时选择真假列值
【发布时间】:2016-10-03 00:41:40
【问题描述】:

我有一个名为 file_download 的表。 我需要显示

  1. 下载的文件列表- select * from file_download where downloaded = 1;
  2. 未下载的文件列表 - select * from file_download where downloaded = 0;
  3. 所有文件列表 - 对于所有文件列表,必须提到什么样的条件?

下面是示例表。

+----+----------+---------+------------+
| pk | filename |  file   | downloaded |
+----+----------+---------+------------+
|  1 | aaa.txt  | aaa.txt |          1 |
|  2 | bbb.txt  | aaa.txt |          1 |
|  3 | ccc.txt  | aaa.txt |          0 |
|  4 | ccc.txt  | aaa.txt |          1 |
|  5 | ccc.txt  | aaa.txt |          0 |
|  6 | ccc.txt  | aaa.txt |          0 |
+----+----------+---------+------------+

提前谢谢...

【问题讨论】:

  • 嗯,如果downloaded只能是0或1,那么就不要使用where标准。如果它可以包含另一个值,请使用 in -- where downloaded in (0,1)...
  • @seddes,没有IN,我们还有其他选择吗?
  • WHERE downloaded IN (0,1) 是最简洁的方式。你也可以使用WHERE downloaded = 0 OR downloaded = 1,但这太长了。但是,如果下载的只有两个可能的值,则无论如何都没有理由使用WHERE

标签: mysql where-in


【解决方案1】:

有三种方法可以做到这一点

使用 OR(成本更低)

从 file_download 中选择 *,其中已下载 = 1 或已下载 = 0

使用IN(简短而准确的方式)

select * from file_download where download in (0, 1);

使用不为空(不推荐方式)

select * from file_download 其中下载不为空

【讨论】:

    【解决方案2】:

    感谢您的回复... 我已经完成了

    select * from file_download where 下载 ?

    ? - 用于随机输入值 (0, 1)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 2018-11-09
      • 2016-09-01
      • 1970-01-01
      • 2013-08-08
      • 2019-08-20
      • 1970-01-01
      相关资源
      最近更新 更多