List:General Discussion« Previous MessageNext Message »
From:Mark Goodge Date:March 30 2011 8:39am
Subject:Re: ` vs '
View as plain text  
On 30/03/2011 09:05, Brent Clark wrote:
> Hiya
>
> Im wondering if someone could help me understand this. If you look at my
> two queries below. By the ORDER BY one is using ` and the other ', as a
> result, if you do an explain you will see that the top query does a
> filesort, while the other does not.

Because column names either need to be unquoted or enclosed in 
backticks. If you put a string inside ordinary quotes (either single or 
double) then it's treated as a string variable. And you can't sort by a 
string variable.

As a demonstration, try these:

SELECT * FROM contacts LIMIT 10
SELECT * FROM `contacts` LIMIT 10
SELECT * FROM 'contacts' LIMIT 10

The first two will work. The third will fail, as you can't select from a 
variable.

Alternatively, try this:

SELECT id FROM contacts LIMIT 10
SELECT `id` FROM contacts LIMIT 10
SELECT 'id' FROM contacts LIMIT 10

and all will be even more clear :-)

Mark
-- 
  http://mark.goodge.co.uk
  http://www.ratemysupermarket.com
Thread
` vs 'Brent Clark30 Mar
  • Re: ` vs 'Simcha Younger30 Mar
  • Re: ` vs 'Mark Goodge30 Mar