Subselect working on dev machine, but not live, with the same database, and both running mysql 5.1
This query is working perfectly on my dev machine, a PC with mysql 5.1.53
SELECT DISTINCT *
FROM posts P1
WHERE user_id IN (2,1000001) AND NOT track_id = 34 AND
(SELECT COUNT(*) FROM posts P2
WHERE P2.user_id = P1.user_id AND P2.id > P1.id AND P2.track_id <> 34)
<= 1
GROUP BY
track_id
ORDER BY
id desc LIMIT 5
when i run the very same piece of code in the same database on my live server (debian, mysql 5.1.39), i get this error:
Unknown column 'P1.user_id' in 'where clause':
How could this be? Any ideas?
Result of show create table posts
on the server
CREATE TABLE `posts` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`track_id` int(11) unsigned DEFAULT '0',
`tag_id` int(11) unsigned DEFAULT NULL,
`user_id` int(11) DEFAULT '0',
`comment` tinytext,
`created_at` datetime DEFAULT NULL,
`commentcount` tinyint(11) unsigned DEFAULT '0',
PRIMARY KEY (`id`),
KEY `track_id` (`track_id`),
KEY `tag_id`开发者_开发知识库 (`tag_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=484 DEFAULT CHARSET=utf8 |
As a lot of comments suggested, it was an issue with case sensitivity. In the SQL query on the server, the alias of posts was set to "p1", but the subselect was looking for "P1". Mistake.
精彩评论