1

こんにちはpoepleは、私がサイトに対して行っている検索機能のために、レコードのリストを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 に答える