我有查詢,看起來像這樣:MySQL的選擇與WHERE條件基於計數
SELECT * FROM `bas_HCCxmlSettings`
WHERE `waarde3` = 'Oke'
OR `waarde4` = 'Oke'
AND `config` = 'XMLserver'
現在我想添加特殊條件如果waarde2 != 0
計數則條件waarde2 = 'int'
其他waarde2 = 'ext'
這是什麼我想:
嘗試1:
SELECT * FROM `bas_HCCxmlSettings`
WHERE `waarde3` = 'Oke'
OR `waarde4` = 'Oke'
AND `config` = 'XMLserver'
AND `waarde2`=
(
(
SELECT count(*)
FROM `bas_HCCxmlSettings`
WHERE `config` = 'XMLserver' AND`waarde2`= 'int' AND
(`waarde3` = 'Oke'
OR `waarde4` = 'Oke'
)
) > 0
,'int'
,'ext'
)
個
try2:
if (SELECT count(*) FROM `bas_HCCxmlSettings` WHERE `waarde2`= 'int' AND (`waarde4` = 'Oke' OR `waarde3`= 'Oke')) > 0
THEN
SELECT count(*) FROM `bas_HCCxmlSettings` WHERE `waarde2`= 'int' AND (`waarde4` = 'Oke' OR `waarde3`= 'Oke')
else
SELECT count(*) FROM `bas_HCCxmlSettings` WHERE `waarde2`= 'ext' AND (`waarde4` = 'Oke' OR `waarde3`= 'Oke')
END
我究竟做錯了什麼?
一些澄清在我的標準查詢爲主查詢
的selecetion標準必須: 1.配置=「XMLserver」 2. Waarde3或waarde4或兩者每畝是「歐客」
查詢不waard2的標準應該是:
SELECT * FROM `bas_HCCxmlSettings`
WHERE `config` = 'XMLserver'
AND`waarde2`= 'int'
AND (
`waarde3` = 'Oke'
OR
`waarde4` = 'Oke'
)
爲了威懾waarde2我選擇requerments的價值是:
- 配置= 'XMLserver'
- waarde2 = INT
- Waarde3或waarde4或雙方必須是 '奧凱'
計數查詢將被:
SELECT count(*) FROM `bas_HCCxmlSettings`
WHERE `config` = 'XMLserver'
AND`waarde2`= 'int'
AND (
`waarde3` = 'Oke'
OR
`waarde4` = 'Oke'
)
如果此選項的計數爲0 then waarde = ext 如果此選擇的計數較大,則0 waarde = int
我注意到,您要添加的條件waarde2' = 0,但是您會在try1> 0的條件和try2 – 2014-09-19 14:18:16
你是什麼意思的第一件事'如果waarde2!= 0'的計數? 'waarde2'是一列。你想計算一次是否有列表'waarde2 ='int''? – 2014-09-19 14:19:17
請注意,您需要使用括號來使您的「OR」以您想要的方式工作。即「A或B和C」被解析爲「A或(B和C)」而不是「(A或B)和C」 – Bohemian 2014-09-19 14:20:00