我想写一个MySql查询,通过它我想使用AND和OR的组合。我有6个属性,其中我希望出现一个String,并且有一个属性应该出现一个强制线程。
我的命令是这样的。
Select * from Students where roll_no like '__2011%' and subject1 ='maths' or subject2='maths' or subject3='maths' or subject4='maths' or subject5='maths' or subject6='maths';
我希望"数学"至少出现在其中一个科目属性中。根据查询中的指定,除此之外,Roll no中还应包含2011。请帮帮我。
Select * from Students where roll_no like '__2011%' and (subject1 ='maths' or subject2='maths' or subject3='maths' or subject4='maths' or subject5='maths' or subject6='maths');
当您有多个or
条件时,in
是一种更干净的处理方式:
Select *
from Students
where
roll_no like '__2011%' and
'maths' in (subject1, subject2, subject3, subject4, subject5, subject6);
这可能比多次"或"快一点
select * from students where roll_no like '__2011%' and
concat_ws(' ',subject1,subject2,subject3,subject4,subject5,subject6) like '%maths%'