1

嗨,人们正在尝试将记录列表放入一个表中,以实现我正在为网站执行的搜索功能

3个表格,1个页面信息,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

4 回答 4

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 
于 2012-12-10T19:59:10.337 回答
1

您的第一个WHERE子句是)没有匹配的左括号的右括号!

于 2012-12-10T20:01:11.683 回答
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
于 2012-12-10T20:02:29.383 回答
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

于 2013-02-05T15:31:05.937 回答