Considering the following table:
CREATE TABLE `foo`(
`aaa` VARCHAR(45) NOT NULL,
`bbb` VARCHAR(45) NOT NULL,
`ccc` INT NOT NULL,
`ddd` INT NOT NULL,
PRIMARY KEY(`aaa`, `bbb`, `ccc`)
);
the following data:
INSERT INTO `foo` (`aaa`, `bbb`, `ccc`, `ddd`)
VALUES
('qwe', 'rty', 0, 123),
('asd', 'fgh', 0, 456),
('asd', 'fgh', 1, 678);
the following result is to be gotten:
'qwe', 'rty', 0, 123
'asd', 'fgh', 1, 678
The objective is to get
- Only one record for each (
aaa,bbb) keypair - Favour records with
ccc= 1 if there are more than 1 record for a (aaa,bbb) keypair in the table
So I think I need to concatenate result sets of 2 queries:
SELECT * FROM `foo` WHERE `ccc` = 1
and
SELECT * FROM `foo` WHERE `ccc` = 0
discarding a second query result row when there is already a same (aaa, bbb) keypair row in the first query result.
How to code it in SQLite?
Try this one,
SQLFiddle Demo