所以我试图在MySQL中使用Union加入4个子查询,我无法弄清楚。
以下是4个子查询。我需要将前2个与最后2个结合起来。
当我尝试UNION 4时,我收到语法错误。我是否需要更改子查询的代码?
SELECT Title, AlbumDescription FROM Albums WHERE MATCH(AlbumDescription) AGAINST('blondes'); SELECT Title, AlbumDescription FROM Albums WHERE MATCH(AlbumDescription) AGAINST('bodybags'); SELECT Title, AlbumDescription FROM Albums WHERE MATCH(AlbumDescrption) AGAINST('fireplaces' WITH QUERY EXPANSION); SELECT Title, AlbumDescription FROM Albums WHERE MATCH(AlbumDescription) AGAINST('sighing' WITH QUERY EXPANSION);
SELECT Title, AlbumDescription FROM Albums WHERE MATCH(AlbumDescription) AGAINST('blondes') SELECT Title, AlbumDescription FROM Albums WHERE MATCH(AlbumDescription) AGAINST('bodybags') UNION SELECT Title, AlbumDescription FROM Albums WHERE MATCH(AlbumDescription) AGAINST('fireplaces' WITH QUERY EXPANSION) SELECT Title, AlbumDescription FROM Albums WHERE MATCH(AlbumDescription) AGAINST('sighing' WITH QUERY EXPANSION);
我使用了上面的语法,我收到一条错误,说我在第3行附近有语法错误。出于某种原因,在这里语法中有AGAINST的缩进,但它不在我的实际代码中。
答案 0 :(得分:0)
为什么需要使用UNION?
查询全部针对同一个表;将它作为对表的单个SELECT。使用AND
和/或OR
逻辑运算符组合谓词。
从表中获取满足任何条件的行:
SELECT a.Title
, a.AlbumDescription
FROM Albums a
WHERE MATCH(a.AlbumDescription) AGAINST('blondes')
OR MATCH(a.AlbumDescription) AGAINST('bodybags')
OR MATCH(a.AlbumDescription) AGAINST('fireplaces' WITH QUERY EXPANSION)
OR MATCH(a.AlbumDescription) AGAINST('sighing' WITH QUERY EXPANSION)
以下是UNION
设置运算符的示例,用于组合四个查询的结果:
SELECT a.Title
, a.AlbumDescription
FROM Albums a
WHERE MATCH(a.AlbumDescription) AGAINST('blondes')
UNION
SELECT a.Title
, a.AlbumDescription
FROM Albums a
WHERE MATCH(a.AlbumDescription) AGAINST('bodybags')
UNION
SELECT a.Title
, a.AlbumDescription
FROM Albums a
WHERE MATCH(a.AlbumDescription) AGAINST('sighing' WITH QUERY EXPANSION)
UNION
SELECT a.Title
, a.AlbumDescription
FROM Albums a
WHERE MATCH(a.AlbumDescription) AGAINST('sighing' WITH QUERY EXPANSION)