我正在查询数据库,我写的查询无效。我研究过使用谷歌,但没有取得任何进展。求救!
${$name} = array();
${$name} = $db->query('SELECT', "
SELECT
SUM(IF(`date`=‘2014-01’, `value`, 0.00)) as `Jan_2014`,
SUM(IF(`date`=‘2014-02’, `value`, 0.00)) as `Feb_2014,
SUM(IF(`date`=‘2014-03’, `value`, 0.00)) as `Mar_2014`,
SUM(IF(`date`=‘2014-04’, `value`, 0.00)) as `Apr_2014`,
SUM(IF(`date`=‘2014-05’, `value`, 0.00)) as `May_2014`,
SUM(IF(`date`=‘2014-06’, `value`, 0.00)) as `Jun_2014`
FROM reporting.$name a
INNER JOIN
reporting.Locations b
ON
a.store_name=b.city
$where
", 'array');
} catch (Exception $e) {
echo $e->getMessage();
}
我收到了错误消息......
SQL错误:SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便在' 2014-01',
value
,0.00)附近使用正确的语法Jan_2014
,SUM(IF(date
} ='2014'在行
答案 0 :(得分:1)
请勿在一年中使用这些卷曲引号,而是使用反引号。
答案 1 :(得分:1)
将引号字符从'更改为
答案 2 :(得分:1)
问题在于您正在使用的'
符号。
${$name} = array();
${$name} = $db->query('SELECT', "
SELECT
SUM(IF('date'='2014-01', 'value', 0.00)) as 'Jan_2014',
SUM(IF('date'='2014-02', 'value', 0.00)) as 'Feb_2014',
SUM(IF('date'='2014-03', 'value', 0.00)) as 'Mar_2014',
SUM(IF('date'='2014-04', 'value', 0.00)) as 'Apr_2014',
SUM(IF('date'='2014-05', 'value', 0.00)) as 'May_2014',
SUM(IF('date'='2014-06', 'value', 0.00)) as 'Jun_2014'
FROM reporting.$name a
INNER JOIN
reporting.Locations b
ON
a.store_name=b.city
$where
", 'array');
} catch (Exception $e) {
echo $e->getMessage();
}