2012-12-30 194 views
0

我得到這個錯誤:這個SELECT語句有什麼問題?

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 '' JOIN `shirts_link` ON `shirts_link`.`shirt_id`=`shirts`.`id` JOIN `shirt_sizes' at line 1

我已經梳理了它近一個半小時,現在並不能弄明白。有人可以指出我嗎?這是代碼。

SELECT 
`shirts`.`shirt_name`, 
`shirts`.`men` AS `main_photo`, 
GROUP_CONCAT (`shirt_sizes`.`size_name`) AS `sizes` 
FROM 
`shirts` 
JOIN 
`shirts_link` ON `shirts_link`.`shirt_id`=`shirts`.`id` 
JOIN 
`shirt_sizes` ON `shirt_sizes`.`id`=`shirts_link`.`size_id` 
JOIN 
`shirt_prices` ON `shirt_prices`.`id`=`shirts_link`.`price_id` 
WHERE `men`!='' 
GROUP BY 
`shirt_prices`.`price_cat` 

如果問題比這個腳本更深,這裏是我試圖綁定在一起的其他表。

軍表

CREATE TABLE shirts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL, 
    shirt_name VARCHAR(20) NOT NULL, 
    men VARCHAR(10) NULL, 
    women VARCHAR(10) NULL, 
    boys VARCHAR(10) NULL, 
    girls VARCHAR(10) NULL, 
    babies VARCHAR(10) NULL, 
)ENGINE=INNODB; 

INSERT INTO shirts(shirt_name,men,women,boys,girls,babies) VALUES 
    ('Crewneck Tee','me_crn','wo_crn','bo_crn','gi_crn','ba_crn'), 
    ('V-Neck Tee','me_vnc','wo_vnc','','',''), 
    ('Scoop Neck Tee','','wo_sco','','',''), 
    ('Raglan Tee','me_rag','wo_rag','bo_rag','gi_rag',''), 
    ('Ringer Tee','me_rin','wo_rin','bo_rin','gi_rin',''), 
    ('Cap Sleeve Tee','','wo_cap','','gi_cap',''), 
    ('Tank Top','me_tan','wo_tan','bo_tan','gi_tan',''), 
    ('Spaghetti Strap','','wo_spa','','',''), 
    ('Hoodie','me_hod','wo_hod','bo_hod','gi_hod','ba_hod'), 
    ('Onsie','','','','','ba_ons'); 

尺寸表

CREATE TABLE shirt_sizes (
    id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, 
    size_name VARCHAR(10) NOT NULL 
)ENGINE=INNODB; 


INSERT INTO shirt_sizes(size_name) VALUES 
    ('new born'), 
    ('6 months'), 
    ('12 months'), 
    ('18 months'), 
    ('2T'), 
    ('3T'), 
    ('4T'), 
    ('5T'), 
    ('x-small'), 
    ('small'), 
    ('medium'), 
    ('large'), 
    ('1x-large'), 
    ('2x-large'), 
    ('3x-large'), 
    ('4x-large'), 
    ('5x-large'); 

價格表

CREATE TABLE shirt_prices (
    id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, 
    price_cat VARCHAR(10) NOT NULL, 
    price NUMERIC(6,2) NOT NULL 
    )ENGINE=INNODB; 

INSERT INTO shirt_prices(price_cat,price) VALUES 
    ('crn_01','25.40'),('crn_02','26.30'),('crn_03','27.20'),('crn_04','28.10'), 
     ('crn_05','29.11'), 

     ('vnc_01','26.21'),('vnc_02','27.11'),('vnc_03','28.01'),('vnc_04','29.02'), 
     ('vnc_05','30.80'), 

     ('sco_01','28.10'), 

     ('rag_01','29.22'),('rag_02','30.44'),('rag_03','31.70'),('rin_01','29.22'), 
     ('rin_02','30.44'),('rin_03','31.70'), 

     ('cap_01','29.04'),('cap_02','30.26'), 

     ('tan_01','25.31'),('tan_02','26.21'), 

     ('spa_01','26.30'),('spa_02','27.11'), 

     ('hod_01','35.21'),('hod_02','36.11'),('hod_03','37.10'),('hod_04','38.11'); 

襯衫鏈接表

CREATE TABLE shirts_link (
     adult VARCHAR(1) NOT NULL, 
     kids VARCHAR(1) NOT NULL, 
     babies VARCHAR(1) NOT NULL, 
     shirt_id INT UNSIGNED NOT NULL, 
     size_id INT UNSIGNED NOT NULL, 
     price_id INT UNSIGNED NOT NULL, 
     PRIMARY KEY (shirt_id,size_id,price_id), 
     FOREIGN KEY (`shirt_id`) REFERENCES `shirts`(`id`), 
     FOREIGN KEY (`size_id`) REFERENCES shirt_sizes(`id`), 
     FOREIGN KEY (`price_id`) REFERENCES shirt_prices(`id`) 
    )ENGINE=INNODB; 

    INSERT INTO shirts_link (adult, kids, babies, shirt_id, size_id, price_id) VALUES 
    ('n','n','y','1','1','1'),('n','n','y','1','2','1'),('n','n','y','1','3','1'),('n','n','y','1','4','1'), 
    ('n','n','y','1','5','1'),('n','n','y','1','6','1'),('n','n','y','1','7','1'),('n','n','y','1','8','1'), 
    ('n','y','n','1','9','1'),('y','y','n','1','10','1'),('y','y','n','1','11','1'),('y','y','n','1','12','1'), 
    ('y','y','n','1','13','1'),('y','n','n','1','14','2'),('y','n','n','1','15','3'),('y','n','n','1','16','4'), 
    ('y','n','n','1','17','5'), 

    ('y','n','n','2','10','6'),('y','n','n','2','11','6'),('y','n','n','2','12','6'),('y','n','n','2','13','6'), 
    ('y','n','n','2','14','7'),('y','n','n','2','15','8'),('y','n','n','2','16','9'),('y','n','n','2','17','10'), 

    ('y','n','n','3','10','11'),('y','n','n','3','11','11'),('y','n','n','3','12','11'),('y','n','n','3','13','11'), 
    ('y','n','n','3','14','11'), 

    ('y','y','n','4','10','12'),('y','y','n','4','11','12'),('y','y','n','4','12','12'),('y','y','n','4','13','12'), 
    ('y','n','n','4','14','13'),('y','n','n','4','15','14'), 

    ('y','y','n','5','10','15'),('y','y','n','5','11','15'),('y','y','n','5','12','15'),('y','y','n','5','13','15'), 
    ('y','n','n','5','14','16'),('y','n','y','5','15','17'), 

    ('y','y','n','6','10','18'),('y','y','n','6','11','18'),('y','y','n','6','12','18'),('y','y','n','6','13','18'), 
    ('y','n','n','6','14','19'), 

    ('y','y','n','7','10','20'),('y','y','n','7','11','20'),('y','y','n','7','12','20'),('y','y','n','7','13','20'), 
    ('y','n','n','7','14','21'), 

    ('y','n','n','8','10','22'),('y','n','n','8','11','22'),('y','n','n','8','12','22'),('y','n','n','8','13','22'), 
    ('y','n','n','8','14','23'), 

    ('n','n','y','9','5','24'),('n','n','y','9','6','24'),('n','n','y','9','7','24'),('n','n','y','9','8','24'), 
    ('n','n','y','9','9','24'),('y','y','n','9','10','24'),('y','y','n','9','11','24'),('y','y','n','9','12','24'), 
    ('y','y','n','9','13','24'),('y','y','n','9','14','25'),('y','n','n','9','15','26'),('y','n','n','9','16','27'), 
    ('y','n','n','9','16','25'), 

    ('n','n','y','10','1','1'),('n','n','y','10','2','1'),('n','n','y','10','3','1'),('n','n','y','10','4','1'); 

回答

2

你在錯誤的地方這個WHERE條款:

WHERE `men`!='' 

移動它在你的GROUP BY條款的前面。

UPDATE:這是您的查詢的重排版本:

SELECT `shirts`.`shirt_name` 
     , `shirts`.`men` AS `main_photo` 
     , GROUP_CONCAT (`shirt_sizes`.`size_name`) AS `sizes` 
FROM `shirts` 
JOIN `shirts_link` 
ON  `shirts_link`.`shirt_id`=`shirts`.`id` 
JOIN `shirt_sizes` 
ON  `shirt_sizes`.`id`=`shirts_link`.`size_id` 
JOIN `shirt_prices` 
ON  `shirt_prices`.`id`=`shirts_link`.`price_id' 

WHERE `men` != '' 

GROUP BY `shirt_prices`.`price_cat` 
+0

所以WHERE子句應該放在GROUP BY子句之上? – Optiq

+0

嗯。無論如何,我仍然遇到錯誤。它的語法正確嗎? – Optiq

+0

是的,在最後一個'JOIN'之後和'GROUP BY'子句之前。 – BellevueBob

5

子句屬於整個from子句之後,意思是在所有連接之後。

+0

是它的語法是否正確?我仍然遇到錯誤。我甚至試圖把表名稱放在它的前面,但它沒有任何區別。 – Optiq