2017-01-05 167 views
1

我正在尋找更有效的方式來調試我的SQL查詢,我使用phpMyAdmin進行大多數查詢檢查。下面是一個查詢的例子:檢查SQL條件是否被滿足

SELECT * FROM `schedule` WHERE 
(
    `start` = {$start} OR 
    ((`start` + (`runtime` * 60)) > {$start} AND (`start` + (`runtime` * 60)) <= {$end}) OR 
    (`start` > {$start} AND `start` < {$end}) 
) AND `group_id` = {$group} 

現在考慮到$開始和start都時間戳時,試圖看看哪些條件被滿足(和混亂),有時這些條件可以在大量它很煩人(而不是像這個例子中的3),有沒有辦法快速確定他們中的哪一個被滿足?沒有手動刪除它們中的每一個?

謝謝!

回答

0

不是一個漂亮的解決方案,但你可以在每個條件添加到列的列表:

SELECT *, 
    IF (`start` = {$start}, 1, 0) AS con1, 
    IF (((`start` + (`runtime` * 60)) > {$start} AND (`start` + (`runtime` * 60)) <= {$end}), 1, 0) AS con2, 
FROM `schedule` WHERE 
(
`start` = {$start} OR 
((`start` + (`runtime` * 60)) > {$start} AND (`start` + (`runtime` * 60)) <= {$end}) OR 
(`start` > {$start} AND `start` < {$end}) 
) AND `group_id` = {$group} 
0

你可以把布爾表達式的選擇列表,並給他們有意義的列名。這些表達式將僅針對滿足WHERE子句條件的行進行評估,因此它不是額外的工作。這些表達式在查詢結果集中返回1(true)或0(false)。

SELECT ..., 
    `start` = {$start} AS is_start, 
    ((`start`+(runtime*60)) > {$start} AND (`start`+(runtime*60)) <= {$end}) AS is_overlapping, 
    (`start` > {$start} AND `start` < {$end}) AS is_between_start_and_end 
FROM `schedule` 
WHERE 
(
    `start` = {$start} 
    OR ((`start`+(runtime*60)) > {$start} AND (`start`+(runtime*60)) <= {$end}) 
    OR (`start` > {$start} AND `start` < {$end}) 
) AND group_id = {$group}