mysql statement issue
I have a MySQL statement that works fine in my query program (Navicat) however when I try and put this in a command in crystal it is throwing an error
The statement is
set @rownum=0
Select
@rownum := @rownum +1 as ranknum
, cust_name
, count(distinct callref) as rank
from opencall
where logdate like '%/08/2011%'
and companyname = "McDo开发者_开发问答nald's Nederland"
group by cust_name
order by rank desc
The error is
Failed to retrieve data from database. details 42000 [MYSQL] [ODBC 3.51 Driver] [mysqld-4.0.16] You have an error in your SQL syntax. Check the manual that corresponds with your MYSQL server version for the right syntax to use near 'Select @rownum :=@rownum +1 as ranknum,cust_name, count(dist [Database Vendor Code:1064 ]
You need to separate statements using a ;
set @rownum=0;
SELECT
@rownum := @rownum +1 as ranknum
, cust_name
, COUNT(distinct callref) as rank
FROM opencall
WHERE logdate LIKE '%/08/2011%' <<-- ?? dates are stored YYYY/MM/DD in a DB.
AND companyname = "McDonald's Nederland"
GROUP BY cust_name
ORDER BY ranknum DESC <<-- ranknum, **not** rank
Alternatively you can do the initialization in a join:
SELECT
@rownum := @rownum +1 as ranknum
, cust_name
, COUNT(distinct callref) as rank
FROM opencall
INNER JOIN (SELECT @rownum := 0) AS initializaton
WHERE logdate LIKE '%/08/2011%'
AND companyname = "McDonald's Nederland"
GROUP BY cust_name
ORDER BY ranknum DESC
Are you sure you didn't mean:
SELECT
@rownum := @rownum +1 as ranknum
, cust_name
, COUNT(distinct callref) as rank
FROM opencall
CROSS JOIN (SELECT @rownum := 0) AS initializaton
WHERE logdate BETWEEN '2011-08-01' AND '2011-08-31'
AND companyname LIKE "McDonald's Nederland" <<-- like is case-insensitive
GROUP BY cust_name <<-- `=` can be case sensitive
ORDER BY ranknum DESC
精彩评论