查询MySQL查询中的语法错误

时间:2011-11-30 02:06:58

标签: mysql sql mysql-error-1064

这是我的疑问:

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的佼佼者。

5 个答案:

答案 0 :(得分: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语句来返回相同的结果。

答案 1 :(得分:1)

在您的所有SELECT SUM(...)中,您未定义FROM player_items AS i

答案 2 :(得分:1)

有问题: -

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

正确: -

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

答案 3 :(得分: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

答案 4 :(得分: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)