如果mysql查询条件失败,我需要显示偏差汇总。我需要显示哪个数据输入是错误的,因为我没有得到任何结果。
更多的是合乎逻辑的问题,所以试图用谷歌搜索同样的问题。
表字段: id、日期、日期、时间、节目、标题
查询
SELECT Count(id) FROM `table`
WHERE day='Monday' and date = '2018-10-15' and time = '19:45:34' and title = 'NEW-BOTTLE'如果满足all条件,上述查询将返回count。我需要显示偏差汇总,以防任何条件值未得到满足。
例如,如果新瓶子输入为NEW-BOTTLE1,那么我需要显示标题名称是不正确的。
以同样的方式,我需要显示哪个值是错误的,因为发生了哪个计数为0。
发布于 2019-08-16 19:25:01
您可以使用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。希望它能以任何方式帮助你。
发布于 2019-08-16 21:10:49
您可以使用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";
}https://stackoverflow.com/questions/57522357
复制相似问题