根据任务编写查询。这个问题问:“列出所有从事初学者或中级活动的非美国地点”。
这是我的查询
SELECT DISTINCT Location.LCountry, Activity.ALevel
FROM Location, Activity
WHERE LCountry != 'USA'
AND ALevel = 'Beginner' OR ALEVEL' = 'Intermediate';
运行此查询时,我仍然会得到一个包含美国值的列表。当我不包含OR ALevel = 'Intermediate"
子句时,它会打印出正确的查询。
答案 0 :(得分:2)
在括号中使用OR condition
SELECT DISTINCT Location.LCountry, Activity.ALevel
FROM Location inner join Activity on Location.LCountry=Activity.Country
WHERE LCountry != 'USA'
AND (ALevel = 'Beginner' OR ALEVEL = 'Intermediate');
或者您可以使用如下所示的IN运算符
SELECT DISTINCT Location.LCountry, Activity.ALevel
FROM Location inner join Activity on Location.LCountry=Activity.Country
WHERE LCountry != 'USA'
AND ALevel in( 'Beginner' , 'Intermediate');
答案 1 :(得分:1)
您需要使用ANSI-92类型的JOIN并使用正确的引号作为:
SELECT DISTINCT l.LCountry, a.ALevel
FROM Location l
LEFT JOIN Activity a
ON ( l.location = a.location )
WHERE l.LCountry != 'USA'
AND ( a.ALevel = 'Beginner' OR a.ALEVEL = 'Intermediate');
顺便说一句,别名使查询更易于阅读。
或者使用分组来获得如下不同的结果:
SELECT l.LCountry, a.ALevel
FROM Location l
LEFT JOIN Activity a
ON ( l.location = a.location )
WHERE l.LCountry != 'USA'
AND ( a.ALevel = 'Beginner' OR a.ALEVEL = 'Intermediate')
GROUP BY l.LCountry, a.ALevel;
答案 2 :(得分:0)
不同位置的单引号不会相应放置或放错位置。
因此请尝试以下查询
SELECT DISTINCT Location.LCountry, Activity.ALevel
FROM Location, Activity
WHERE Location.LCountry <> 'USA'
AND Activity.ALevel IN ('Beginner','Intermediate')
答案 3 :(得分:0)
使用更新后的查询:
SELECT DISTINCT Location.LCountry, Activity.ALevel
FROM Location, Activity
WHERE LCountry != 'USA'
AND (ALevel = 'Beginner' OR ALEVEL' = 'Intermediate');