简体   繁体   English

如何正确编写SQL查询

[英]How can I properly write SQL query

I am trying to write a SQL querybut am not sure how to properly do what I want to achieve. 我正在尝试编写SQL查询,但不确定如何正确地完成我想实现的目标。 Right now I have 现在我有

select id 
from job 
where status = 'NE' 
    or status = 'RU' 
    and queue = CONCAT('VA-', varVappId) 
    or queue = CONCAT('CL-', varClientId, '||', 'VA-', varVappId);

so basically if the job has a status of NE or a status of RU and queue matches either concat string then it should produce a result. 因此,基本上,如果作业的状态为NE或RU且队列与concat字符串匹配,则它将产生结果。

You should use parentheses to group together clauses. 您应该使用括号将子句组合在一起。

select id 
from job 
where 
    (status = 'NE' or status = 'RU')
    and (queue = CONCAT('VA-', varVappId) 
    or queue = CONCAT('CL-', varClientId, '||', 'VA-', varVappId));

Missing Parenthesis. 缺少括号。

select id 
from job 
where (status = 'NE' 
    or status = 'RU' )
    and (queue = CONCAT('VA-', varVappId) 
    or queue = CONCAT('CL-', varClientId, '||', 'VA-', varVappId));

or 要么

select id 
from job 
where status in( 'NE','RU' )
    and (queue = CONCAT('VA-', varVappId) 
    or queue = CONCAT('CL-', varClientId, '||', 'VA-', varVappId));

In your where condition the OR condition should seperated from AND using (),So the OR condition will check first and it will combine with AND 在您的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));

There are some parameters missing, needed to group clauses. 缺少一些参数,需要对子句进行分组。 Also this is the general format, if that's something you needed too. 这也是一般格式,如果您也需要这种格式。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM