我試圖找出一種方法來查詢我的架構在sqlfillde與if子句,基本上我需要檢查表是否有行匹配如果標籤==?或者如果name ==?,那麼下面的查詢就完成了工作。在情況下,如果sqlfiddle滯後,我就會把該方案之下查詢,如果,如果子句返回false mysql
id name label description
1 Grams G Used to measure light weight items
1 Kilograms G Used to measure heavy weight items
3 M Liters ML Used to measure liquids
4 Grams G L Used to measure liquids
SELECT * FROM `catutype` WHERE label = "G" OR name = "Grams"
但如果匹配的記錄的ID == 2,我wan't排除記錄。所以我試過這個,
SELECT * FROM `catutype` WHERE id!= 2 AND label = "G" OR name = "Grams"
正如你所看到的,它沒有成功。我得到的唯一的解決辦法是通過一個子查詢
SELECT *,(SELECT label FROM catutype WHERE name = "Grams") as "type" FROM catutype WHERE catutype.id != 2
但我依然無法運行兩個子奎雷斯檢查標籤有比賽,以使其更清晰,我會寫一個簡單的僞代碼,我需要在mysql中。
if(id!=2)
{
check if label = ? OR name = ?
}
謝謝。
謝謝@Nikhil,解釋清楚。 – droiddude
歡迎@droiddude :) –