I want to select the number of users who marked some content as favorites, and also return if the current user has "voted" or not. My table looks like this:
CREATE TABLE IF NOT EXISTS `favorites` (
`user` int(11) NOT NULL DEFAULT '0',
`content` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`user`,`content`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
Let's say I have 3 lines containing
INSERT INTO `favorites` (`user`, `content`) VALUES
(11, 26977),
(22, 26977),
(33, 26977);
Using this
SELECT COUNT(*), CASE
WHEN user='22'
THEN 1
ELSE 0
END as has_voted
FROM favorites WHERE content = '26977'
I expect to receive has_voted=1and COUNT(*)=3but
I get has_voted=0and COUNT(*)=3. Why is this? How to fix it?
daker source
share