mysql 为什么我在使用子选择执行此选择时会收到错误1064?

tgabmvqs  于 2023-03-07  发布在  Mysql
关注(0)|答案(2)|浏览(129)

我的任务

给定的SQL创建了一个Song表并插入了一些歌曲。第一个SELECT语句选择1992年以后发行的歌曲。第二个SELECT语句选择ID为800的歌曲的发行年份。
创建组合两个现有查询的第三个查询。第一个SELECT应为外部查询,第二个SELECT应为子查询。ORDER BY子句应出现在子查询之后。

ID INT,
  Title VARCHAR(60),
  Artist VARCHAR(60),
  ReleaseYear INT,
  Genre VARCHAR(20),
  PRIMARY KEY (ID)
);

INSERT INTO Song VALUES
  (100, 'Hey Jude', 'Beatles', 1968, 'pop rock'),
  (200, 'You Belong With Me', 'Taylor Swift', 2008, 'country pop'),
  (300, 'You\'re Still the One', 'Shania Twain', 1998, 'country pop'),
  (400, 'Need You Now', 'Lady Antebellum', 2011, 'country pop'),
  (500, 'You\'ve Lost That Lovin\' Feeling', 'The Righteous Brothers', 1964, 'R&B'),
  (600, 'That\'s The Way Love Goes', 'Janet Jackson', 1993, 'R&B'),
  (700, 'Smells Like Teen Spirit', 'Nirvana', 1991, 'grunge'),
  (800, 'Even Flow', 'Pearl Jam', 1992, 'grunge'),
  (900, 'Black Hole Sun', 'Soundgarden', 1994, 'grunge');

SELECT *
FROM Song
WHERE ReleaseYear > 1992
ORDER BY ReleaseYear;

SELECT ReleaseYear
FROM Song 
WHERE ID = 800;

□ □ □目前为止我的尝试

SELECT ReleaseYear
FROM Song
WHERE ReleaseYear > 1992
   (Select ReleaseYear
   FROM Song 
   WHERE ID = 800)
Order by ReleaseYear DESC;

我不知道我的查询和子查询出了什么问题。每次我试图运行它时,我得到:ERROR 1064,表示它不理解我的代码。

d4so4syb

d4so4syb1#

若要获取1992之后发行的歌曲以及ID为800的歌曲的发行年份,可以使用UNION ALL运算符合并两个SELECT查询的结果:

SELECT ReleaseYear 
FROM Song 
WHERE ReleaseYear > 1992
UNION ALL
SELECT ReleaseYear 
FROM Song 
WHERE ID = 800;

db〈〉小提琴output

g6ll5ycj

g6ll5ycj2#

说明不清楚,但我怀疑预期的查询是:

SELECT *
FROM Song
WHERE ReleaseYear > 1992
OR ReleaseYear = (
    SELECT ReleaseYear
    FROM Song
    WHERE ID = 800
)
ORDER BY ReleaseYear;

相关问题