2013-01-23 19 views
0

任何人都知道爲什麼下面的查詢會產生錯誤MySQL錯誤 - 「檢查語法使用近「%s的」

「#1064 - 你在你的SQL語法錯誤;檢查對應手冊,您用於在'%s'附近使用正確語法的MySQL服務器版本「)?

SELECT SQL_CALC_FOUND_ROWS id 
FROM (
    SELECT taba.id 
FROM (

SELECT alum.id 
FROM cvm_education AS edu 
JOIN cvm_alumni AS alum ON alum.id = edu.alumni_id 
WHERE cvm_alumni.profile_status =1 
AND highest_edu 
IN (

SELECT name 
FROM cvm_filter_educationlevels 
JOIN cvm_educationlevel AS edulevels ON educationlevel_id = edulevels.id 
WHERE filter_id = % s 
) 
) AS taba 

乾杯!

+1

你缺少一個右括號 - 你有三公,只有兩個關閉 – bhttoan

+0

注意到了,但不知道從哪裏最後支架 – nv39

+1

添加@ nv39這樣的事') AS taba)xxx' –

回答

1

試試這個:

SELECT count(taba.id) 
FROM (

SELECT alum.id 
FROM cvm_education AS edu 
JOIN cvm_alumni AS alum ON alum.id = edu.alumni_id 
WHERE alum.profile_status =1 
AND highest_edu 
IN (

SELECT name 
FROM cvm_filter_educationlevels 
JOIN cvm_educationlevel AS edulevels ON educationlevel_id = edulevels.id 
WHERE filter_id = 1 
) 
) AS taba ; 

http://www.sqlfiddle.com/#!2/f8adc/15

ŧ WO要點:

  1. 我不明白,使用SQL_CALC_FOUND_ROWS的()如果計數已偶然 它()。我認爲這提供了相同的期望結果。
  2. 您尚未提供樣本數據,因此我無法嘗試%s。我有 用二進制(1,0)代替它。此外,我不知道你的 確切的代碼,所以我根據你的查詢做了一些假設。

的樣本數據:

CREATE TABLE cvm_education(
    ID int auto_increment primary key, 
    alumni_id int 
); 

CREATE TABLE cvm_alumni(
    ID int auto_increment primary key, 
    profile_status int, 
    highest_edu varchar(30) 
); 

CREATE TABLE cvm_filter_educationlevels (
    ID int auto_increment primary key, 
    educationlevel_id int, 
    name varchar(30) 
); 

CREATE TABLE cvm_educationlevel(
    ID int auto_increment primary key, 
    filter_id int 
); 

INSERT INTO cvm_education (alumni_id) 
VALUES (10), (1), (2), (3),(5), (6),(7),(8),(9); 

INSERT INTO cvm_alumni (profile_status, highest_edu) 
VALUES (1, "master"), 
(0,"bachelor"), 
(1,"bachelor"), 
(0, "master"), 
(1, "master"), 
(0, "master"), 
(1, "master"), 
(1, "master"), 
(1, "master"), 
(1, "master"); 

INSERT INTO cvm_filter_educationlevels(educationlevel_id,name) 
VALUES (1, "master"), (0,"bachelor"); 

INSERT INTO cvm_educationlevel(filter_ID) 
VALUES (1), (0), (1), (0), (0), (1),(1),(1),(1); 
3

你需要引用的字符串值,並使用LIKE模式匹配

WHERE filter_id LIKE '% s' 

,但如果你真的想找到% s從字面上看,使用=

WHERE filter_id = '% s' 
+0

嗨JW - 感謝您的快速回復。當我添加單引號時,我得到幾乎相同的錯誤:#1064 - 你的SQL語法有錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以便在第4行「'附近使用正確的語法。 – nv39

+0

@ nv39您只能執行此查詢嗎? 'SELECT taba.id \t \t FROM \t \t \t( \t \t \t \t \t SELECT alum.id \t \t \t \t \t FROM AS cvm_education EDU \t \t \t \t \t \t INNER JOIN cvm_alumni AS明礬 \t \t \t \t \t \t \t ON alum.id = edu.alumni_id \t \t \t \t \t WHERE cvm_alumni。PROFILE_STATUS = 1個AND \t \t \t \t \t \t highest_edu IN \t \t \t \t \t \t( \t \t \t \t \t \t \t \t SELECT NAME \t \t \t \t \t \t \t \t FROM cvm_filter_educationlevels \t \t \t \t \t \t \t \t \t INNER JOIN cvm_educationlevel AS edulevels \t \t \t \t \t \t \t \t \t \t ON educationlevel_id = edulevels.id \t \t \t \t \t \t \t \t WHERE過濾器_id = '%s' 的 \t \t \t \t \t \t) \t \t \t)AS taba'? –

+0

nope,同樣的錯誤 – nv39

1

的「% s」是無效的語法。如果這是一個文字,那麼就需要用引號括起來:

WHERE filter_id = '% s' 

(但是,修復不似乎是正確的它幾乎看起來好像正在用的sprintf生成MySQL的聲明,並在那裏。意欲是本來應該用價值來代替一個「%s」佔位符)

此外,還有一個右括號和別名從語句的結束丟失:

) foo 

而且這樣的:

WHERE cvm_alumni.profile_status = 1 

應該改成這樣:

WHERE alum.profile_status = 1 

(該表被分配了一個別名,列引用應與別名加以限定,而不是表名)

這也是一個不錯的主意以限定參考文獻的所有列參考,包括educationlevel_id,highest_eduname。 (這不一定是這個陳述的問題,除非MySQL拋出一個「模棱兩可的列」錯誤,但我更喜歡將我的陳述與任何「模棱兩可」的錯誤隔離開來,當添加新列時會出現錯誤。)


SELECT SQL_CALC_FOUND_ROWS id 
    FROM (SELECT taba.id 
      FROM (
       SELECT alum.id 
        FROM cvm_education edu 
        JOIN cvm_alumni alum 
        ON alum.id = edu.alumni_id 
       WHERE alum.profile_status = 1 
        AND `highest_edu` IN 
         (
         SELECT `name` 
          FROM cvm_filter_educationlevels 
          JOIN cvm_educationlevel edulevels 
          ON `educationlevel_id` = edulevels.id 
         WHERE `filter_id` = '% s' 
         ) 
       ) taba 
     ) foo 

+0

嗨,謝謝。我在AS taba之前添加了引號,並進行了改變。我得到這個錯誤:#1248 - 每個派生表都必須有它自己的別名 – nv39

+0

也試着在括號後加上括號和相同的錯誤 – nv39