MySQL查询使用concat和union无法正常工作

时间:2012-12-10 19:58:14

标签: mysql union concat

嗨,我正在尝试将记录列表放入一个表中,以便为网站进行搜索功能

3个表格用于页面信息,1个用于项目,1个用于最新新闻。

但是我的查询中出现错误,我看不出它有什么问题。

SELECT 'section' AS colTYPE, title_sec AS title,
CONCAT_WS(' ',contentcol1_sec, contentcol2_sec, contentcol3_sec) AS content, 
active_sec AS active 
    FROM sections_sec 
    WHERE content LIKE 'fff' OR title LIKE 'fff' AND active = 1) 
    UNION(SELECT 'latest' AS colTYPE, title_lat AS title,
    content_lat AS content, active_lat AS active 
    FROM latest_lat
    WHERE content LIKE 'fff' OR title LIKE 'fff' AND active = 1) 
    UNION(SELECT 'project' AS colTYPE, title_prj AS title, 
    content_prj AS content,     active_prj AS active 
    FROM projects_prj 
    WHERE content LIKE 'fff' OR title LIKE 'fff' AND active = 1)

我正在接受的错误是:

You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near ') 
    UNION(SELECT 'latest' AS colTYPE, title_lat AS title, 
content_lat AS conten' at line 3

4 个答案:

答案 0 :(得分:2)

您的第一个陈述没有开括号。通常没有必要在UNION

的括号中包含您的语句

您是否也确定不想使用UNION ALL

SELECT 'section'
           AS colTYPE, 
       title_sec
           AS title, 
       Concat_ws(' ', contentcol1_sec, contentcol2_sec, contentcol3_sec)
           AS content, 
       active_sec
           AS active 
FROM   sections_sec 
WHERE  content LIKE 'fff' 
        OR title LIKE 'fff' 
           AND active = 1 
UNION ALL
SELECT 'latest'    AS colTYPE, 
       title_lat   AS title, 
       content_lat AS content, 
       active_lat  AS active 
FROM   latest_lat 
WHERE  content LIKE 'fff' 
        OR title LIKE 'fff' 
           AND active = 1 
UNION ALL
SELECT 'project'   AS colTYPE, 
       title_prj   AS title, 
       content_prj AS content, 
       active_prj  AS active 
FROM   projects_prj 
WHERE  content LIKE 'fff' 
        OR title LIKE 'fff' 
           AND active = 1 

答案 1 :(得分:1)

您的第一个WHERE子句正在关闭括号)而没有匹配的左括号!

答案 2 :(得分:1)

试试这个:

SELECT 'section' AS colTYPE, title_sec AS title, CONCAT_WS(' ',contentcol1_sec, contentcol2_sec, contentcol3_sec) AS content, active_sec AS active 
    FROM sections_sec 
    WHERE (content LIKE 'fff' OR title LIKE 'fff') AND active = 1
    UNION SELECT 'latest' AS colTYPE, title_lat AS title, content_lat AS content, active_lat AS active 
    FROM latest_lat
    WHERE (content LIKE 'fff' OR title LIKE 'fff') AND active = 1 
    UNION SELECT 'project' AS colTYPE, title_prj AS title, content_prj AS content, active_prj AS active 
    FROM projects_prj 
    WHERE (content LIKE 'fff' OR title LIKE 'fff') AND active = 1

答案 3 :(得分:0)

在第一个子查询中,您必须使用HAVING,以便可以匹配计算值。在其他子查询中,您需要使用WHERE子句中的列而不是别名。

SELECT 'section' AS colTYPE, title_sec AS title,
CONCAT_WS(' ',contentcol1_sec, contentcol2_sec, contentcol3_sec) AS content, 
active_sec AS active 
FROM sections_sec 
HAVING content LIKE 'fff' OR title LIKE 'fff' AND active = 1
UNION
SELECT 'latest' AS colTYPE, title_lat AS title,
content_lat AS content, active_lat AS active 
FROM latest_lat
WHERE content_lat LIKE 'fff' OR title_lat LIKE 'fff' AND active_lat = 1
UNION
SELECT 'project' AS colTYPE, title_prj AS title, 
content_prj AS content, active_prj AS active 
FROM projects_prj 
WHERE content_prj LIKE 'fff' OR title_prj LIKE 'fff' AND active_prj = 1

此外,请确保您了解条件表达式中AND和OR混合的优先级。 x OR y OR z被解析为x OR (y AND z),而不是(x OR y) AND z