简体   繁体   English

SQL - 如何在 WHERE 子句中避免多个 OR 运算符?

[英]SQL - How to avoid multiple OR operators in WHERE clause?

sqlite> .schema movie
CREATE TABLE movie (
  id INTEGER PRIMARY KEY, title TEXT, year INTEGER, nth TEXT, for_video BOOLEAN
  );
sqlite>
sqlite>
sqlite>

For the question:对于这个问题:

Which movie(s), not counting movies whose titles start with punctuation, come first in alphabetical order, and which come last?哪些电影(不包括标题以标点符号开头的电影)按字母顺序排在第一位,哪些排在最后?

below syntax:下面的语法:

SELECT title 
FROM movie 
WHERE NOT(title LIKE '%!%' or title LIKE '%#%' or title LIKE '%$%')  
ORDER BY title ASC LIMIT 1;

has never ending or operators.有永无止境的或运营商。


Syntax should work in mysql and sqlite语法应适用于 mysql 和 sqlite

How to avoid multiple OR operators that check for punctuations?如何避免检查标点符号的多个OR运算符? yet to add more OR operators for punctuation check...尚未添加更多OR运算符用于标点检查...

For th first part (multiple ORs).对于第一部分(多个 OR)。 Also note that the syntax is different for the join ( Like )另请注意,连接的语法不同( Like

You can create a table for punctuations and add the data into it您可以为标点符号创建一个表格并将数据添加到其中

 Create table Punctuations (punckSign varchar(10))

 insert into Punctuations
 values ('!'), ('#'), ('$')

Then left join that with movie table as follows然后将其与电影表左连接,如下所示

MySQL MySQL

 SELECT title FROM movie m
 Left join Punctuations p 
 on  title  like concat('%',p.punckSign,'%')  
 where p.punckSign is null

SQLite SQLite

 SELECT title FROM movie m
 Left join Punctuations p 
 on  title  like '%'|| p.punckSign || '%'  
 where p.punckSign is null

Try the below using REGEXP使用 REGEXP 尝试以下操作

SELECT title 
FROM movie 
WHERE title not REGEXP '!|#|$'
ORDER BY title ASC LIMIT 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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