Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MySql query error, can anyone equivalent query of this?
- select posts.id, posts.title
- from posts
- inner join (
- select post_id,max(created)
- from comments
- group by post_id
- order by max(created) DESC ) as foo
- on posts.id=foo.post_id
- order by foo.max(created) DESC;
- ERROR 1630 (42000): FUNCTION foo.max does not exist.
- Check the 'Function Name Parsing and Resolution' section in the Reference Manual
- mysql> describe comments;
- +----------+--------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+--------------+------+-----+---------+----------------+
- | id | int(11) | NO | PRI | NULL | auto_increment |
- | post_id | int(11) | NO | MUL | NULL | |
- | name | varchar(255) | NO | | NULL | |
- | email | varchar(255) | NO | | NULL | |
- | body | varchar(500) | NO | | NULL | |
- | mark | tinyint(4) | NO | | 1 | |
- | created | datetime | YES | | NULL | |
- | modified | datetime | YES | | NULL | |
- +----------+--------------+------+-----+---------+----------------+
- 8 rows in set (0.00 sec)
- mysql> describe posts;
- +-------------+--------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------------+--------------+------+-----+---------+----------------+
- | id | int(11) | NO | PRI | NULL | auto_increment |
- | user_id | int(11) | NO | | NULL | |
- | title | varchar(255) | NO | | NULL | |
- | body | text | YES | | NULL | |
- | category_id | int(11) | NO | | NULL | |
- | tags | varchar(50) | NO | | NULL | |
- | mark | tinyint(4) | NO | | 1 | |
- | created | datetime | YES | | NULL | |
- | modified | datetime | YES | | NULL | |
- +-------------+--------------+------+-----+---------+----------------+
- 9 rows in set (0.00 sec)
- select posts.id, posts.title
- from posts
- inner join (
- select post_id, max(created) as most_recent
- from comments
- group by post_id) as foo
- on posts.id=foo.post_id
- order by most_recent DESC;
- foo.max(created)
- max(foo.created)
- select posts.id, posts.title
- from posts
- inner join
- (
- select post_id,max(created) AS created
- from comments
- group by post_id
- ) as foo
- on posts.id=foo.post_id
- order by created DESC; <-- you don't need max
- SELECT posts.id,
- posts.title
- FROM posts
- INNER JOIN (SELECT post_id,
- created
- FROM comments
- GROUP BY post_id
- ORDER BY Max(created) DESC) AS foo
- ON posts.id = foo.post_id
- ORDER BY Max(foo.created) DESC;
Add Comment
Please, Sign In to add comment