· 8 years ago · Aug 12, 2018, 11:22 AM
1Is this result set possible with a left or right join?
2CREATE TABLE IF NOT EXISTS `questions` (
3`id` int(10) unsigned NOT NULL auto_increment,
4`question` text NOT NULL,
5PRIMARY KEY (`id`)
6) ENGINE=InnoDB;
7
8CREATE TABLE IF NOT EXISTS `answers` (
9`fb_user` int(3) unsigned NOT NULL,
10`question_id` int(10) unsigned NOT NULL,
11`answer` text NOT NULL,
12UNIQUE KEY `fb_user` (`fb_user`,`question_id`)
13) ENGINE=InnoDB;
14
15CREATE TEMPORARY TABLE answers_f SELECT * from answers WHERE fb_user = '$fb_profile_id'
16SELECT questions.*, answers_f.* FROM answers_f RIGHT JOIN questions ON questions.id = answers_f.question_id ORDER BY questions.id
17
18SELECT
19 questions.id,
20 questions.question,
21 answers.answer
22FROM questions
23LEFT JOIN answers
24ON question_id = questions.id
25AND fb_user = 'foobar'
26ORDER BY questions.id