I have a news table as following:
CREATE TABLE `news` (
`title` varchar(255) NOT NULL,
`hash` char(40) NOT NULL,
PRIMARY KEY (`hash`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And, another votes table as following:
CREATE TABLE `votes` (
`hash` char(40) NOT NULL,
`user_id` varchar(255) NOT NULL,
`vote_type` enum('up','down') DEFAULT NULL,
PRIMARY KEY (`hash`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Now I want to get something like this when a user logged in-
this_is_a_title hash_12312312 NULL
this_is_a_title hash_12312313 up
this_is_a_title hash_12312314 NULL
this_is_a_title hash_12312315 down
Can you suggest an optimal single query for this?
I think my question was not clear. Sorry for that. By NULL field I meant a new which doesn't has any vote casted from this user or anybody.
My version is -
SELECT news.*, votes.vote_type
FROM news
LEFT OUTER JOIN votes
ON votes.`hash` = news.`hash`
AND votes.`user_id` = #