2011-11-30 535 views
1

這是我的查詢:查詢語法錯誤

SELECT 
    `i`.`itemtype` AS `Item`, 
    `p`.`name` 
    (SELECT SUM(`i`.`count`) AS `Count` WHERE `itemtype` = 2148), 
    (SELECT SUM(`i`.`count`) * 100 AS `Count1` WHERE `itemtype` = 2152), 
    (SELECT SUM(`i`.`count`) * 10000 AS `Count2` WHERE `itemtype` = 2160) 
FROM `player_items` AS `i` 
LEFT JOIN `players` AS `p` ON (`p`.`id` = `i`.`player_id`) 
WHERE `i`.`itemtype` IN (2148, 2152, 2160) 
GROUP BY `i`.`itemtype` 
LIMIT 0, 30 

當我運行在MySQL上面的查詢,我得到這個錯誤信息:

#1064 - 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 
'SELECT SUM(`i`.`count`) AS `Count` WHERE `itemtype` = 2148) 
    , (SELECT SUM(`i`' at line 4 

我完全不明白是什麼這是否意味着我是MySQL中的一個begginer。

回答

2

你錯過此列後一個逗號:

`p`.`name` 

考慮這個查詢,而不是:

SELECT i.itemtype AS Item 
    , p.name 
    , SUM(CASE 
      WHEN itemtype = 2148 THEN i.count 
      ELSE 0 
     END) AS Count 
    , SUM(CASE 
      WHEN itemtype = 2152 THEN i.count 
      ELSE 0 
     END) * 100 AS Count1 
    , SUM(CASE 
      WHEN itemtype = 2160 THEN i.count 
      ELSE 0 
     END) * 10000 AS Count2 
FROM player_items AS i 
LEFT JOIN players AS p ON i.player_id = p.id 
WHERE i.itemtype IN (2148, 2152, 2160) 
GROUP BY i.itemtype ASC 
LIMIT 0, 30 

總結這三列在一起,你可以:

SELECT t.itemType AS Item, t.Count, t.Count1, t.Count2 
    , SUM(t.Count1, t.Count2, t.Count3) AS Total 
FROM 
(
    SELECT i.itemtype AS Item 
     , p.name 
     , SUM(CASE 
       WHEN itemtype = 2148 THEN i.count 
       ELSE 0 
      END) AS Count 
     , SUM(CASE 
       WHEN itemtype = 2152 THEN i.count 
       ELSE 0 
      END) * 100 AS Count1 
     , SUM(CASE 
       WHEN itemtype = 2160 THEN i.count 
       ELSE 0 
      END) * 10000 AS Count2 
    FROM player_items AS i 
    LEFT JOIN players AS p ON i.player_id = p.id 
    WHERE i.itemtype IN (2148, 2152, 2160) 
    GROUP BY i.itemtype ASC 
    LIMIT 0, 30 
) AS t 

或者你也可以添加其他CASE

SELECT i.itemtype AS Item 
    , p.name 
    , SUM(CASE 
      WHEN itemtype = 2148 THEN i.count 
      ELSE 0 
     END) AS Count 
    , SUM(CASE 
      WHEN itemtype = 2152 THEN i.count 
      ELSE 0 
     END) * 100 AS Count1 
    , SUM(CASE 
      WHEN itemtype = 2160 THEN i.count 
      ELSE 0 
     END) * 10000 AS Count2 
    , SUM(CASE 
      WHEN itemtype = 2148 THEN i.count 
      WHEN itemtype = 2152 THEN (i.count * 100) 
      WHEN itemtype = 2160 THEN (i.count * 10000) 
      ELSE 0 
     END) AS Total 
FROM player_items AS i 
LEFT JOIN players AS p ON i.player_id = p.id 
WHERE i.itemtype IN (2148, 2152, 2160) 
GROUP BY i.itemtype ASC 
LIMIT 0, 30 

CASE statements,便無需您必須運行三個額外的SELECT語句才能返回相同的結果。

+0

感謝您的好解決方案。順便說一句。是否有可能在另一行中將'Count','Count1'和'Count2'加在一起? – Cyclone

+0

@Cyclone我已經更新了我的答案,以解決您評論中的問題。 –

+0

謝謝:-)))。 – Cyclone

1

在所有的SELECT SUM(...)你不定義FROM player_items AS i

1

故障: -

SELECT SUM(`i`.`count`) AS `Count` <-- missing FROM which table 

正確: -

SELECT SUM(`i`.`count`) AS `Count` FROM some_table ... 
1

我想你想是這樣的:

SELECT p.name 
     ,i.itemtype 
     ,CASE itemtype 
     WHEN 2148 THEN count(*) 
     WHEN 2152 THEN count(*) * 100 
     WHEN 2160 THEN count(*) * 10000 
     END AS ct 
FROM players p 
LEFT JOIN player_items i ON p.id = i.player_id 
WHERE i.itemtype IN (2148, 2152, 2160) 
GROUP BY p.name, i.itemtype 
ORDER BY p.name, i.itemtype 
LIMIT 30; 

或者,也許這樣的:

SELECT p.name 
     ,sum(CASE WHEN itemtype = 2148 THEN 1 ELSE 0 END) AS ct 
     ,sum(CASE WHEN itemtype = 2152 THEN 1 ELSE 0 END) * 100 AS ct1 
     ,sum(CASE WHEN itemtype = 2160 THEN 1 ELSE 0 END) * 10000 AS ct2 
FROM players p 
LEFT JOIN player_items i ON p.id = i.player_id 
WHERE i.itemtype IN (2148, 2152, 2160) 
GROUP BY p.name 
ORDER BY p.name 
LIMIT 30; 

看到一個working demo for this version

1
SELECT 
    `i`.`itemtype` AS `Item`, 
    `p`.`name *<{miss ','}>* 
    (SELECT SUM(`i`.`count`) AS `Count` *<miss from 'table'>* WHERE `itemtype` = 2148), 
    (SELECT SUM(`i`.`count`) * 100 AS `Count1` *<miss from 'table'>* WHERE `itemtype` = 2152), 
    (SELECT SUM(`i`.`count`) * 10000 AS `Count2` *<miss from 'table'>* WHERE `itemtype` = 2160)