SQL如何查询出三门成绩任意一门=100且任意一门=66的所有学生?
成绩表:姓名语文成绩数学成绩英语成绩
张三598889
李四825690
王二6663100
如何查询出三门成绩任意一门=100且任意一门=66的所有学生? select * from table where (A=100 or B=100 or C=100) and (A=66 or B=66 or C=66) 楼上正解括号里或括号外和 WITH src AS (
SELECT Name,Y AS 'C'
FROM dbo.Score
UNION ALL
SELECT Name,S
FROM dbo.Score
UNION ALL
SELECT Name,E
FROM dbo.Score
)
SELECT DISTINCT src.Name
FROM src
WHERE src.C =100 OR src.C = 66 通过临时表with的这个查询会把成绩有66或100的都查出来,不止成绩有66且100的情况。
idNameYSE
1张三6889100
2李四666623
3王五6637100
根据这个sql修改了一下,不过感觉还是那个(or)and(or)的更方便。
WITH src AS (
SELECT Name,Y AS 'C'
FROMdbo.Score
UNION ALL
SELECT Name,S
FROMdbo.Score
UNION ALL
SELECT Name,E
FROMdbo.Score
)
SELECT src.Name, count(IF(src.C=100,1,null)) as score1, count(IF(src.C=66,1,null)) as score2
FROM src
GROUP BY src.Name
HAVING score1>=1 AND score2>=1
页:
[1]