Page 1 of 1

Error in my SQL syntax - Blog Tutorial

Posted: Mon Jun 20, 2011 8:08 am
by nyo
Hi,

I am following the tutorial about creating a blog and I am on part 3. I tested my SQL code in phpmyadmin just as you show in the video but it gave me this error:

#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 '. 'post_id' AS 'id', 'posts' . 'post_title' AS 'title', LEFT('post' at line 2

And here is my code:
SELECT
	'posts' . 'post_id' AS 'id',
	'posts' . 'post_title' AS 'title',
	LEFT('posts' . 'post_body', 128) AS 'preview',
	DATE_FORMAT('posts' . 'post_date', '%d/%m/%Y %H:%i:%s') AS 'date',
	'comments' . 'total_comments',
	DATE_FORMAT('comments' . 'last_comment', '%d/%m/%Y %H:%i:%s') AS 'last_comment'
	FROM 'posts'
	LEFT JOIN (
	SELECT
		'post_id',
		COUNT('comment_id') AS 'total_comments',
		MAX('comment_date') AS 'last_comment'
		FROM 'comments'
		GROUP BY 'post_id'
) AS 'comments'
	ON 'posts' . 'post_id' = 'comments' . 'post_id'
	ORDER BY 'posts' . 'post_date' DESC

Re: Error in my SQL syntax - Blog Tutorial

Posted: Mon Jun 20, 2011 10:25 am
by Tino
You should use backticks ( ` ) around table and field names, whereas you are using single quotes ( ' ).

Why do so many people do this wrong?

Re: Error in my SQL syntax - Blog Tutorial

Posted: Mon Jun 20, 2011 10:38 am
by JelvinJS7
Tino wrote:Why do so many people do this wrong?
Probably cuz single quotes are used in just about everything else in php, and this is the only time you see backticks. It males sense to an inexperienced coder that you use single quotes there.

Re: Error in my SQL syntax - Blog Tutorial

Posted: Mon Jun 20, 2011 10:47 am
by Tino
Well sure. But if you view it in full screen HD, it's quite easy to see the difference :?

Oh well.

Re: Error in my SQL syntax - Blog Tutorial

Posted: Mon Jun 20, 2011 10:55 am
by nyo
Tino wrote:You should use backticks ( ` ) around table and field names, whereas you are using single quotes ( ' ).

Why do so many people do this wrong?
I tried that as well before posting this, but it didn't work. I will try it once more to make sure.

EDIT: Ok, it worked now. Thanks a lot!

Re: Error in my SQL syntax - Blog Tutorial

Posted: Mon Jun 20, 2011 12:28 pm
by jacek
Using the large version of the player on youtube on my laptop screen I can see the difference pretty clearly.

I will try yo remember to mention it though ;)