SELECT基于多结果条件

时间:2014-04-06 06:33:06

标签: sql database postgresql

我试图在PostgreSQL 9.2中执行以下SQL语句,但它失败并出现错误:

SELECT "TrainingName" FROM "HSETrainingName" WHERE "ID" = 
SELECT "trainingID" FROM "HSEGroupedTraining" WHERE "groupID" =3

第二个select语句返回多个值。怎么解决?

2 个答案:

答案 0 :(得分:2)

您可以使用IN构建:

SELECT "TrainingName" 
  FROM "HSETrainingName" 
 WHERE "ID" IN (SELECT "trainingID" 
                  FROM "HSEGroupedTraining" 
                 WHERE "groupID" = 3)

答案 1 :(得分:0)

使用子查询和IN:

SELECT "TrainingName" 
FROM "HSETrainingName" 
WHERE "ID" IN
(SELECT "trainingID" 
FROM "HSEGroupedTraining" 
WHERE "groupID" =3)