【问题标题】:SQL, Checking if all record's value greater than 5 in a table [closed]SQL,检查表中所有记录的值是否大于 5 [关闭]
【发布时间】:2015-06-02 16:31:33
【问题描述】:

我正在使用 PHP,我需要一个 sql 查询来检查所有记录的值是否大于 5.. 不会从数据库返回任何数据。如果所有记录都大于 5,则将返回 true。

如果答案是真的,我会更新另一个表..

伪代码是

if(the value column in table1 is greater then 5 for all records)
then
    $update = mysql_query("UPDATE table2 SET q_value = '0' ")
else
do nothing


Table Example:
------------------------

| question |  value   |
|    X1    |     6    |
|    X2    |     6    |
|    X3    |     6    | //value column for all records are greater then 5 
|    X4    |     7    | //must return true
|    X5    |     8    | //and will update table2
|    X6    |     6    |

| question |  value   |
|    X1    |     6    |
|    X2    |     3    |
|    X3    |     0    | //value column for all records not greater then 5 
|    X4    |     7    | //there are 0 and 3 values and must return false
|    X5    |     8    | //won't update table2 
|    X6    |     6    |

你能给我一个想法吗?如何检查所有记录。不会返回任何数据。如果所有值都大于某个值,则将返回 true...

提前谢谢你

【问题讨论】:

  • 你能展示你的实际代码吗?
  • 大于> - 大于或等于>=
  • 无法添加答案,但看起来您正在寻找 IF NOT EXISTS(从 Table1 WHERE 值
  • 到目前为止我还没有听说过 if not exists 命令。但我尝试了这个查询,它可以工作。非常感谢。

标签: php mysql sql-server


【解决方案1】:
select count(*)
from table1
where value <= 5

如果返回 0,则为 TRUE,否则为 FALSE。

【讨论】:

  • 非常感谢您。这对我有帮助..
猜你喜欢
  • 2018-11-25
  • 2017-12-03
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 2012-12-07
  • 2021-07-13
  • 2018-06-01
  • 2015-06-18
相关资源
最近更新 更多