[英]Finding which condition Failed in MYSQL Query (Deviation Summary)
如果任何mysql查询条件出现问题,我需要显示偏差摘要。 我需要显示由于没有得到任何结果而导致输入错误的数据。
更多的逻辑问题,所以试图谷歌搜索相同。
表字段:id,日期,日期,时间,程序,标题
询问
SELECT Count(id) FROM `table`
WHERE day='Monday' and date = '2018-10-15' and time = '19:45:34' and title = 'NEW-BOTTLE'
如果满足所有条件,上述查询将返回count。 如果任何“条件”值不满意,我需要显示“偏差摘要”。
例如,如果将NEW-BOTTLE输入为NEW-BOTTLE1,则需要显示标题名称不正确。
同样,我需要显示由于发生了哪个计数0而导致哪个值是错误的。
您可以使用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`
它将根据条件满足返回1
或0
。 希望它对您有任何帮助。
您可以使用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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.