开发者

mysql query results sorting

开发者 https://www.devze.com 2023-01-03 21:37 出处:网络
I am calling the results form a query to a list on my site based on if the item is \"downtown_hosted\". This works fine but I would now like to sort that list DESC, but can\'t seem to get the syntax c

I am calling the results form a query to a list on my site based on if the item is "downtown_hosted". This works fine but I would now like to sort that list DESC, but can't seem to get the syntax correct.

Below is what I have:

$result_events = mysql_query("SELECT * FROM events 
           开发者_开发问答                     WHERE downtown='downtown_hosted'
                                ORDER BY date DESC
                                LIMIT 5 ");


You need to escape the word "date" with backticks.

E.g.:

$result_events = mysql_query("
    SELECT * FROM events
    WHERE downtown='downtown_hosted'
    ORDER BY `date` DESC
    LIMIT 5
");

In practice it's not a bad habit to get into always enclosing your columns with backticks, so you will not have to worry about conflicting with language keywords.


date is an SQL keyword. You can have a column called date, but every time you refer to it you will have to use identifier quotes. In MySQL this is accomplished using backticks: `date`

0

精彩评论

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