简体   繁体   中英

Is this SQL query syntax correct?

SELECT DATEDIFF(dd, (SELECT date FROM player WHERE 
date = (SELECT MAX(date) FROM player WHERE name = 'Deuslegio') 
AND name = 'Deuslegio'), CURDATE())

Basically, all I am trying to do is get the latest row record's date of a certain player name and getting the difference from today's date. But according to the phpMyAdmin on GoDaddy, I am getting this error:

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 ' CURDATE())' at line 1

You are using three parameters in your DATEDIFF function:

 1. dd 
 2. (SELECT DATE FROM player WHERE DATE = (SELECT MAX(DATE) FROM player WHERE NAME = 'Deuslegio')   AND NAME = 'Deuslegio')
 3. CURDATE()

You may only use two.

mySQL DATEDIFF()

You can simplify your query a lot. This is your query (as fixed by removing the dd :

SELECT DATEDIFF((SELECT date
                 FROM player
                 WHERE date = (SELECT MAX(date) FROM player WHERE name = 'Deuslegio'
                              ) AND name = 'Deuslegio'
                ), CURDATE()
               )

The following is equivalent:

SELECT DATEDIFF((SELECT date
                 FROM player
                 WHERE name = 'Deuslegio'
                 order by date desc
                 limit 1 
                ), CURDATE()
               )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM