![](/img/trans.png)
[英]How can I write this SQL query properly in Rails without declaring the association?
[英]How can I properly write SQL query
我正在嘗試編寫SQL查詢,但不確定如何正確地完成我想實現的目標。 現在我有
select id
from job
where status = 'NE'
or status = 'RU'
and queue = CONCAT('VA-', varVappId)
or queue = CONCAT('CL-', varClientId, '||', 'VA-', varVappId);
因此,基本上,如果作業的狀態為NE或RU且隊列與concat字符串匹配,則它將產生結果。
您應該使用括號將子句組合在一起。
select id
from job
where
(status = 'NE' or status = 'RU')
and (queue = CONCAT('VA-', varVappId)
or queue = CONCAT('CL-', varClientId, '||', 'VA-', varVappId));
缺少括號。
select id
from job
where (status = 'NE'
or status = 'RU' )
and (queue = CONCAT('VA-', varVappId)
or queue = CONCAT('CL-', varClientId, '||', 'VA-', varVappId));
要么
select id
from job
where status in( 'NE','RU' )
and (queue = CONCAT('VA-', varVappId)
or queue = CONCAT('CL-', varClientId, '||', 'VA-', varVappId));
在您的where條件中, OR
條件應使用() AND
分開,因此OR條件將首先進行檢查,並將與AND組合
select id
from job
where (status = 'NE' or status = 'RU' ) and
(queue = CONCAT('VA-', varVappId) or
queue = CONCAT('CL-', varClientId, '||', 'VA-', varVappId));
缺少一些參數,需要對子句進行分組。 這也是一般格式,如果您也需要這種格式。
SELECT id
FROM job
WHERE status IN( 'NE','RU' )
AND (queue = CONCAT('VA-', varVappId)
OR queue = CONCAT('CL-', varClientId, '||', 'VA-', varVappId));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.