Murphy1976
Murphy1976

Reputation: 1475

What's wrong with this SQL statement - Syntax Error #1064?

SELECT id, content, UNIX_TIMESTAMP(timestamp) AS unixtime 
FROM data
ORDER BY id ASC 
WHERE unixtime <= CURDATE() LIMIT 10;

or

SELECT id, content, UNIX_TIMESTAMP(timestamp) AS unixtime 
FROM data
ORDER BY id ASC 
WHERE unixtime < CURDATE() LIMIT 10;

all phpAdmin is telling me is:

#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 'WHERE unixtime <= CURDATE() LIMIT 10' at line 1

Upvotes: 0

Views: 1626

Answers (4)

BluesRockAddict
BluesRockAddict

Reputation: 15683

Try the following:

SELECT id, content, UNIX_TIMESTAMP(timestamp) AS unixtime 
FROM data 
WHERE UNIX_TIMESTAMP(timestamp) <= CURDATE() 
ORDER BY id ASC LIMIT 10;

Upvotes: 1

Taryn
Taryn

Reputation: 247700

You have the ORDER BY and WHERE clauses reversed.

SELECT id, content, UNIX_TIMESTAMP(timestamp) AS unixtime 
FROM data
WHERE unixtime < CURDATE()
ORDER BY id ASC LIMIT 10;

or

SELECT id, content, UNIX_TIMESTAMP(timestamp) AS unixtime 
FROM data
WHERE unixtime <= CURDATE() 
ORDER BY id ASC LIMIT 10;

I just realized that part of your issue because you are referencing an alias in your WHERE clause.:

WHERE UNIX_TIMESTAMP(timestamp) <= CURDATE() 

or

WHERE UNIX_TIMESTAMP(timestamp) < CURDATE() 

Upvotes: 2

M.I.T.
M.I.T.

Reputation: 1042

try this

SELECT id, content, UNIX_TIMESTAMP(timestamp) AS unixtime 
FROM data
WHERE (UNIX_TIMESTAMP(timestamp)) < CURDATE() ORDER BY id ASC  LIMIT 10;

Upvotes: 1

ristonj
ristonj

Reputation: 1608

You need the where statement before the order by statement.

Upvotes: 0

Related Questions