开发者

having problem with a query

开发者 https://www.devze.com 2023-02-27 05:51 出处:网络
I have a DB with user_id and last_updated SELECT user_id, MAX(last_updated) as timestamp FROM online WHERE user_id > 0

I have a DB with user_id and last_updated

SELECT user_id, MAX(last_updated) as timestamp 
  FROM online 
 WHERE user_id > 0 
 GROUP_BY user_id

I'm getting this error

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 'GROUP_BY user_id' at line 1

SELECT user_id, MAX(last_updated) as timestamp 
  FROM online 
 WHERE user_id > 0 
 GROUP_BY user_id

Someone s开发者_Python百科uggested I use the above query so I'm not sure why I'm getting this error

I googled group_by and don't see what I did wrong. I thought maybe the as timestamp but not sure!


It's GROUP BY not GROUP_BY...

SELECT user_id, MAX(last_updated) as timestamp 
  FROM online 
 WHERE user_id > 0 
 GROUP BY user_id

UPDATE

Per comments, also note that timestamp is a reserved word. You should chance the alias (i.e. max_last_updated) or escape it with backticks (i.e. `timestamp`)


Change the code from

SELECT user_id, MAX(last_updated) as timestamp 
FROM online WHERE user_id > 0 GROUP_BY user_id

to

SELECT user_id, MAX(last_updated) as `timestamp` 
FROM online WHERE user_id > 0 GROUP BY user_id

Reserved words (such as timestamp) can only be used a identifiers if enclosed by backticks '`'.

Also GROUP_BY -> GROUP BY *(Two words, SQL keywords never have a '_' in them)*.

Even better is to not use reserved words as they are confusing (and backticks make my head hurt)


timestamp is a data type used by MySQL, try to change it to some thing else.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号