我正在尝试为高尔夫数据库编写查询。它需要返回statisticID = 1
p2sStatistic > 65
且statisticID = 3
p2sStatistic > 295
{。}}的玩家。
一个statisticID
是驾驶距离,另一个是精确度等等。我尝试了以下但是它不起作用,似乎无法在线找到答案。如果不进行观察,我该怎么做呢?
SELECT playerFirstName, playerLastName
FROM player2Statistic, player
WHERE player.playerID=player2Statistic.playerID
AND player2Statistic.statisticID=statistic.statisticID
AND p2sStatistic.3 > 295
AND p2sStatistic.1 > 65;
http://i.imgur.com/o8epk.png - db的图片
尝试获取它只输出满足这两个条件的玩家列表。
答案 0 :(得分:0)
您缺少查询中的statistic
表。您需要根据where
条款加入。
您还需要使用正确的连接语法。
以下版本在统计表中连接两次,一次为“1”,一次为“3”:
SELECT distinct playerFirstName, playerLastName
FROM player2Statistic p2s join
player p
on p.playerId = p2s.playerId join
statistic s3
on s3.StatisticId = p2s.statistcId and
s3.StatisticId = 3 join
statistic s1
on s1.StatisticId = p2s.statisticId and
s1.StatisticId = 1
WHERE (s3.statistic > 295 and s1.statistic > 65)
答案 1 :(得分:0)
您需要两次加入统计信息表:
SELECT playerFirstName, playerLastName
FROM player p
JOIN player2Statistic s1
on p.playerID=s1.playerID and s1.statisticID = 1
JOIN player2Statistic s3
on p.playerID=s3.playerID and s1.statisticID = 3
WHERE s1.p2sStatistic > 65 and s3.p2sStatistic > 295;
答案 2 :(得分:0)
对于没有重复的的玩家列表,EXISTS
半连接可能是最好的:
SELECT playerFirstName, playerLastName
FROM player AS p
WHERE EXISTS (
SELECT 1
FROM player2Statistic AS ps
WHERE ps.playerID = p.playerID
AND ps.StatisticID = 1
AND ps.p2sStatistic > 65
)
AND EXISTS (
SELECT 1
FROM player2Statistic AS ps
WHERE ps.playerID = p.playerID
AND ps.StatisticID = 3
AND ps.p2sStatistic > 295
);
列名称和上下文源自提供的屏幕截图。问题中的查询并未完全覆盖它 注意括号,它们需要处理运算符优先级。
这可能更快(重复可能不可能):
SELECT p.playerFirstName, p.playerLastName
FROM player AS p
JOIN player2Statistic AS ps1 USING (playerID)
JOIN player2Statistic AS ps3 USING (playerID)
AND ps1.StatisticID = 1
AND ps1.p2sStatistic > 65
AND ps3.StatisticID = 3
AND ps3.p2sStatistic > 295;
如果您的绝密品牌RDBMS不支持SQL标准(USING (playerID)
,请将ON ps1.playerID = p.playerID
替换为相同的效果。
这是关系划分的情况。在此相关问题下,找到许多更多查询技术来处理它:
How to filter SQL results in a has-many-through relation