Page 1 of 1

Error In My Query?

Posted: Mon Apr 29, 2002 7:37 pm
by SpaceManSpiff
I'm getting an error with the query below and I can't seem to find anything wrong with it. Can anyone help?

Code: Select all

SELECT NewsID,NewsTitle FROM News ORDER BY NewsDate LIMIT 3
Error Message:

Code: Select all

Error 
SQL-query: 

SELECT NewsID,NewsTitle FROM News ORDER BY NewsDate LIMIT 3

MySQL said:
It's weird because it doesn't even give a reason for the error after "MySQL said."

Posted: Mon Apr 29, 2002 10:34 pm
by enygma
just be sure that everything is in the right case (MySQL is case sensitive), and, of course, obviously, that all the fields are there.

then, if that still doesn't work right, try running it on the command line MySQL client....and see what that says

-enygma

Posted: Tue Apr 30, 2002 12:21 am
by SpaceManSpiff
I tried using that query on phpMyAdmin, that's how I got that error message. All the cases are right and all the fields are there.

Image

The query works fine when I take off "LIMIT 3." The problem occurs when I have that or "LIMIT 0, 3" at the end. Any ideas?

Posted: Tue Apr 30, 2002 5:21 am
by Oxy

Code: Select all

SELECT NewsID,NewsTitle FROM News ORDER BY NewsDate ASC LIMIT 3
try putting ASC/DESC in between NewsDate and LIMIT

Posted: Tue Apr 30, 2002 7:48 pm
by SpaceManSpiff
Oxy wrote:

Code: Select all

SELECT NewsID,NewsTitle FROM News ORDER BY NewsDate ASC LIMIT 3
try putting ASC/DESC in between NewsDate and LIMIT
I don't understand what that would do, especially since it mixes up different clauses.. but since at this point I'm willing to try anything, it didn't work.

Code: Select all

Error 
SQL-query: 

SELECT NewsID,NewsTitle FROM News ORDER BY NewsDate LIMIT DESC 3
MySQL said: You have an error in your SQL syntax near 'DESC 3' at line 1
Is there any other way to get the three most recent records from a database? I know that in MSSQL, I can do.. "SELECT TOP 3 NewsID,NewsTitle FROM News ORDER BY NewsDate DESC"

Posted: Wed May 01, 2002 3:13 am
by Oxy

Code: Select all

SELECT * FROM News ORDER BY NewsDate DESC LIMIT 3
<edit>Fixed typo

Posted: Wed May 01, 2002 6:21 am
by jason
Oxy, no..

Code: Select all

SELECT NewsID,NewsTitle FROM News ORDER BY NewsID DESC LIMIT 3
That is what you want.

Posted: Wed May 01, 2002 6:22 am
by jason
Also quick question.

Is there any data in the table?

yeah

Posted: Wed May 01, 2002 2:36 pm
by Oxy
yeah sorry typo, but i'm a bit confused as to why the first query I posted didn't work for you.

Posted: Wed May 01, 2002 5:39 pm
by SpaceManSpiff
jason wrote:Also quick question.

Is there any data in the table?
Yea, there's about five records in there, each having a different NewsDate.

Posted: Wed May 01, 2002 11:57 pm
by phice
*sigh*

Code: Select all

SELECT * FROM News ORDER BY NewsID DESC LIMIT 0,3
then, just set $variable = $row['newsID']; or whatever.

Posted: Thu May 02, 2002 12:12 am
by EvilWalrus
phice wrote:*sigh*

Code: Select all

SELECT * FROM News ORDER BY NewsID DESC LIMIT 0,3
then, just set $variable = $row['newsID']; or whatever.
'*' is evil