MySQL SELECT Question

Synkc

Active Member
Messages
1,765
Reaction score
0
Points
36
I've been working on a News Management script for my website, which stores data in a MySQL database. Everything seems to be working perfectly, except for this small problem.

I'm trying to use the following syntax to retrive the first three rows from the database table "site_news" which have a value of "1" in the "show" column, arranged by a timestamp (the date it was posted) in descending order.

Code:
SELECT * FROM site_news WHERE 'show'='1' ORDER BY date_posted DESC LIMIT 3

However, it doesn't seem to work. I'm just wondering whether it is actually possible to preform the action in a single query and whether the syntax I used is set out correctly.


Thanks to anyone who can help. :)
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
Code:
SELECT * FROM site_news WHERE show=1 ORDER BY date_posted DESC LIMIT 3

As soon as you put the quotes around show, it became a string, so it was comparing the string "show" to the string "1" instead of checking if the value of the column show is equal to the number 1 :)


Spoken from experience; the code up there should be enough to fix it ;)
 

Synkc

Active Member
Messages
1,765
Reaction score
0
Points
36
Thanks for the reply Livewire, but it seems the problem was caused because it was phrasing the table name "show" as the MySQL syntax "SHOW" - simply fixed by changing the table's name.

Thanks again for the help, I've sent you some credits. :)
 

altrock182182

New Member
Messages
40
Reaction score
0
Points
0
Another way to fix that is to specify a table or column useing the `Special Quotes` whatever they're called

Code:
SELECT * FROM `DB`.`Table` WHERE `SHOW`=1 LIMIT 3
 
Last edited:
Top