By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

#1248 - Every derived table must have its own alias ,

P: 2
I'm getting error #1248 - Every derived table must have its own alias ,
below is the full msql statement please Help Im newbie many thanks.

Expand|Select|Wrap|Line Numbers
  1. SELECT `Posts`.`Post_id` AS `posts_id`, `Posts`.`title` AS `title`, LEFT(`Posts`.`body`, 512) AS                             `preview`, `comments`.`total_comments` AS `total_comments`, `category`.`Cat_id` AS                             `category_id`, `body`,`category`.`Cat_name`, DATE_FORMAT(`comments`.`last_comments`,                             `%d/%m/%y %H:%i:%s`) AS `last_comments`
  2.                             FROM `Posts`
  3.                             INNER JOIN `category` ON `category`.`Cat_id` = `Posts`.`Cat_id`
  4.                             LEFT JOIN (SELECT `Post_id`, COUNT(`Comm_id`) AS `total_comments`, MAX(`comm_date`) AS `last_comment` FROM `comments`) GROUP BY `comments`.`Post_id` AS `comments`"
Oct 4 '12 #1

✓ answered by rpnew

Hi Kelvinejimogu,

This part in your query should have alias

Expand|Select|Wrap|Line Numbers
  1. SELECT `Post_id`, COUNT(`Comm_id`) AS `total_comments`, MAX(`comm_date`) AS `last_comment` FROM `comments
  2.  
Like
Expand|Select|Wrap|Line Numbers
  1. (SELECT `Post_id`, COUNT(`Comm_id`) AS `total_comments`, MAX(`comm_date`) AS `last_comment` FROM `comments) tab1
  2.  
BTW I don't see condition for your LEFT join either on which you are joining with this query

Share this Question
Share on Google+
3 Replies


rpnew
100+
P: 188
Hi Kelvinejimogu,

This part in your query should have alias

Expand|Select|Wrap|Line Numbers
  1. SELECT `Post_id`, COUNT(`Comm_id`) AS `total_comments`, MAX(`comm_date`) AS `last_comment` FROM `comments
  2.  
Like
Expand|Select|Wrap|Line Numbers
  1. (SELECT `Post_id`, COUNT(`Comm_id`) AS `total_comments`, MAX(`comm_date`) AS `last_comment` FROM `comments) tab1
  2.  
BTW I don't see condition for your LEFT join either on which you are joining with this query
Oct 4 '12 #2

P: 2
Thanks guys it now working as it should be here is the full SQL
Expand|Select|Wrap|Line Numbers
  1. SELECT `Posts`.`Post_id` AS `posts_id`, `Posts`.`title` AS `title`, LEFT(`Posts`.`body`, 512) AS                             `preview`, `comments`.`total_comments` AS `total_comments`, `category`.`Cat_id` AS `category_id`, `body`,`category`.`Cat_name`, DATE_FORMAT(`comments`.`last_comments`, '%d/%m/%Y %H:%i:%s') AS `last_comments`
  2.                             FROM `Posts`
  3.                             INNER JOIN `category` ON `category`.`Cat_id` = `Posts`.`Cat_id`
  4.                             LEFT JOIN (SELECT `Post_id`, COUNT(`Comm_id`) AS `total_comments`, MAX(`comm_date`) AS `last_comments` FROM `comments` GROUP BY `comments`.`Post_id`) AS `comments` ON `Posts`.`Post_id` = `comments`.`Post_id` ORDER BY `Post_date`
Oct 8 '12 #3

rpnew
100+
P: 188
Glad that it helped..
Oct 8 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.