【问题标题】:Finding which condition Failed in MYSQL Query (Deviation Summary)查找MYSQL查询中哪个条件失败(偏差总结)
【发布时间】:2019-08-16 09:42:14
【问题描述】:

如果任何 mysql 查询条件失败,我需要显示偏差摘要。我需要显示哪个数据输入错误,因为我没有得到任何结果。

更多的是一个逻辑问题,所以试图用谷歌搜索同样的问题。

表格字段: id , day , date , time , program , title

查询

SELECT Count(id) FROM `table` 
WHERE day='Monday' and date = '2018-10-15' and time = '19:45:34' and title = 'NEW-BOTTLE'

如果满足所有条件,上述查询将返回计数。我需要显示偏差摘要,以防任何条件值不满足。

例如,如果 NEW-BOTTLE 输入为 NEW-BOTTLE1,那么我需要显示标题名称不正确。

以同样的方式,我需要显示哪个值是错误的,因为发生了计数 0。

【问题讨论】:

    标签: php mysql logic


    【解决方案1】:

    您可以使用if 条件选择值如下:

    SELECT IF(day = 'Monday', 'TRUE','FALSE') as dayResult, IF(date = '2018-10-15', 'TRUE','FALSE') as dateResult,
    IF(time = '19:45:34', 'TRUE','FALSE') as timeResult, IF(title = 'NEW-BOTTLE', 'TRUE','FALSE') as titleResult   FROM `table` 
    

    它将根据条件满足返回10。希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      您可以使用 switch 语句打印详细的摘要。这是伪代码。

      switch (true) {
      
      case 0:
          SELECT Count(id) FROM `table`
          WHERE day = 'Monday' and date = '2018-10-15' and time = '19:45:34' and title = 'NEW-BOTTLE'
          // Get the count from query    
      
          if ($count != '0') {
              // Criteria met successfully, not need to check other cases;
              break;
          }
      
      case 1:
          SELECT Count(id) FROM `table`
          WHERE day = 'Monday' and date = '2018-10-15' and time = '19:45:34'
      
          //get the count
          if ($count != '0')
              echo "Title is incorrect";
      
      case 2:
          SELECT Count(id) FROM `table`
          WHERE day = 'Monday' and date = '2018-10-15' and title = 'NEW-BOTTLE'
          //get the count
          if ($count != '0')
              echo "Time didn't match";
      
      case 3:
          SELECT Count(id) FROM `table`
          WHERE day = 'Monday' and time = '19:45:34' and title = 'NEW-BOTTLE'
          if ($count != '0')
              echo "Date didn't match";
      
      case 4:
          SELECT Count(id) FROM `table`
          WHERE date = '2018-10-15' and time = '19:45:34' and title = 'NEW-BOTTLE'
          if ($count != '0')
              echo "Day didn't match";
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-23
        相关资源
        最近更新 更多