I’m having problems trying to get a query like this
table answers
CREATE TABLE IF NOT EXISTS `rws_answers` (
`id_answer` int(11) NOT NULL AUTO_INCREMENT,
`id_question` int(11) DEFAULT NULL,
`row_subject` varchar(255) NOT NULL,
`column_subject` varchar(255) DEFAULT NULL,
`answer_type` varchar(1) DEFAULT NULL,
`score` int(5) DEFAULT '0',
PRIMARY KEY (`id_answer`),
KEY `IDX_51C8C33DE62CA5DB` (`id_question`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=765 ;
table reviews
CREATE TABLE IF NOT EXISTS `rws_reviews` (
`id_review` int(11) unsigned NOT NULL,
`id_client` varchar(36) NOT NULL DEFAULT '',
`id_item` int(11) unsigned NOT NULL DEFAULT '0',
`id_question` int(11) unsigned NOT NULL DEFAULT '0',
`id_location` int(11) unsigned NOT NULL DEFAULT '0',
`id_answer` int(11) unsigned NOT NULL DEFAULT '0',
`value` text,
`date_answered` datetime NOT NULL,
PRIMARY KEY (`id_review`,`id_client`,`id_item`,`id_question`,`id_location`,`id_answer`),
KEY `IDX_6868EE34943B391C` (`id_item`),
KEY `IDX_6868EE34E62CA5DB` (`id_question`),
KEY `IDX_6868EE34E45655E` (`id_location`),
KEY `IDX_6868EE34FCEAFFC8` (`id_answer`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
what I need is to get all the id_answer in table answers that are and aren’t in table reviews
so if I try this sql
SELECT r.id_review, a.id_answer AS aa, r.id_answer AS ra, a.row_subject, r.id_location
FROM rws_answers a
LEFT JOIN rws_reviews r USING(id_answer)
ORDER BY a.id_answer DESC
I get those rows, but because the reviews table can have different id_location I need to filter them so I add to the query this
SELECT r.id_review, a.id_answer AS aa, r.id_answer AS ra, a.row_subject, r.id_location
FROM rws_answers a
LEFT JOIN rws_reviews r USING(id_answer)
WHERE r.id_location = 1
ORDER BY a.id_answer DESC
but then all my answers that aren’t in reviews table don’t show anymore, I had try any sort of combination without result, can anyone be so kind to try to help me out or maybe suggest me a different way of doing the query, I have more than 3 day trying, readying, doing example, don’t know what else to do, thanks!
You can convert the
USINGclause to anONclause, and merge theWHEREclause into it: