MS Access使用多个UNION语句

时间:2017-11-30 17:46:26

标签: sql ms-access

我在MS Access中有三个表:动画,电影和系列,所有这些表都有名称属性(anm_name,flm_name和srs_name)和流派属性(anm_genre,flm_genre和srs_genre),我尝试了使用UNION将所有共享相同类型值的名称属性包含到单个列中。我用单一类型成功完成了这个,这是我使用的代码:

SELECT 'Anime ' AS [Media Type], anm_name AS [Action] FROM Anime
WHERE anm_genre LIKE "*Action*"
UNION
SELECT 'Film ', flm_name AS [Action] FROM Film
WHERE flm_genre LIKE "*Action*"
UNION
SELECT 'Series ', srs_name AS [Action] FROM Series
WHERE srs_genre LIKE "*Action*";

但当我尝试使用相同的代码结构将另一个列排序到另一列时:

SELECT 'Anime ' AS [Media Type], anm_name AS [Action] FROM Anime
WHERE anm_genre LIKE "*Action*"
UNION
SELECT 'Film ', flm_name AS [Action] FROM Film
WHERE flm_genre LIKE "*Action*"
UNION
SELECT 'Series ', srs_name AS [Action] FROM Series
WHERE srs_genre LIKE "*Action*"

SELECT anm_name AS [Adventure] FROM Anime
WHERE anm_genre LIKE "*Adventure*"
UNION
SELECT flm_name AS [Adventure] FROM Film
WHERE flm_genre LIKE "*Adventure*"
UNION
SELECT srs_name As [Adventure] FROM Series
WHERE srs_genre LIKE "*Adventure*"

我收到以下代码错误: error

这是我的数据库中的示例:Here

预期输出类似于this

我希望所有三个表中的名称记录对应于(例如)具有值Action的类型记录,这些记录将分组在一个名为' Action&#39的列中;和Adventure等相同。 我的问题似乎与我使用已有别名的不同别名列的事实有关。

2 个答案:

答案 0 :(得分:0)

您在第二组联盟中错过了FROM声明:

SELECT anm_name FROM Anime
WHERE anm_genre LIKE "*Adventure*"
UNION
SELECT flm_name AS [Adventure] FROM Film
WHERE flm_genre LIKE "*Adventure*"
UNION
SELECT srs_name As [Adventure] FROM Series
WHERE srs_genre LIKE "*Adventure*"`

答案 1 :(得分:0)

在SQL语句的末尾有一个逗号。

WHERE srs_genre LIKE "*Adventure*"`

应该是

WHERE srs_genre LIKE "*Adventure*"