I have the following table:
+----+-------+---------+---------------+
| id | owner | variant | EAN |
+----+-------+---------+---------------+
| 1 | 101 | black | 1111111111111 |
+----+-------+---------+---------------+
| 2 | 102 | blue | 1111111111112 |
+----+-------+---------+---------------+
| 3 | 103 | white | 1111111111113 |
+----+-------+---------+---------------+
| 4 | 103 | white | 1111111111114 |
+----+-------+---------+---------------+
| 5 | 103 | black | 1111111111115 |
+----+-------+---------+---------------+
| 6 | 104 | white | 1111111111116 |
+----+-------+---------+---------------+
| 7 | 104 | black | 1111111111117 |
+----+-------+---------+---------------+
I want to select all rows except the one's that have the same owner and variant
My output has to be like this:
+----+-------+---------+---------------+
| id | owner | variant | EAN |
+----+-------+---------+---------------+
| 1 | 101 | black | 1111111111111 |
+----+-------+---------+---------------+
| 2 | 102 | blue | 1111111111112 |
+----+-------+---------+---------------+
| 4 | 103 | white | 1111111111114 |
+----+-------+---------+---------------+
| 5 | 103 | black | 1111111111115 |
+----+-------+---------+---------------+
| 6 | 104 | white | 1111111111116 |
+----+-------+---------+---------------+
| 7 | 104 | black | 1111111111117 |
+----+-------+---------+---------------+
Can anyone explain me how this can be done? I have tried group by but that only works on 1 column
Best Answer
The particular EAN to render would be determined by the database.
Assuming your tablename is august15, then
using the sample data below
+----+-------+---------+---------------+ | id | owner | variant | EAN | +----+-------+---------+---------------+ | 1 | 101 | black | 1111111111111 | +----+-------+---------+---------------+ | 2 | 102 | blue | 1111111111112 | +----+-------+---------+---------------+ | 3 | 103 | white | 1111111111113 | +----+-------+---------+---------------+ | 4 | 103 | white | 1111111111114 | +----+-------+---------+---------------+ | 5 | 103 | black | 1111111111115 | +----+-------+---------+---------------+ | 6 | 104 | white | 1111111111116 | +----+-------+---------+---------------+ | 7 | 104 | black | 1111111111117 | +----+-------+---------+---------------+
This query
SELECT * FROM
would resultAugust15
GROUP BY owner, variant;+----+-------+---------+---------------+ | id | owner | variant | EAN | +----+-------+---------+---------------+ | 1 | 101 | black | 1111111111111 | +----+-------+---------+---------------+ | 2 | 102 | blue | 1111111111112 | +----+-------+---------+---------------+ | 5 | 103 | black | 1111111111115 | +----+-------+---------+---------------+ | 3 | 103 | white | 1111111111113 | +----+-------+---------+---------------+ | 7 | 104 | black | 1111111111117 | +----+-------+---------+---------------+ | 6 | 104 | white | 1111111111116 | +----+-------+---------+---------------+
to get exactly what you need change the query to
SELECT * FROM
August15
GROUP BY owner, variant ORDER BY id ASC;Sample Data
SET FOREIGN_KEY_CHECKS=0;
-- Table structure for
august15
DROP TABLE IF EXISTS
august15
; CREATE TABLEaugust15
(id
int(11) NOT NULL AUTO_INCREMENT,owner
int(11) NOT NULL,variant
varchar(50) NOT NULL,ean
varchar(11) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL, PRIMARY KEY (id
) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;-- Records of august15
INSERT INTO
august15
VALUES ('1', '101', 'black', '11111111111'); INSERT INTOaugust15
VALUES ('2', '102', 'blue', '11111111112'); INSERT INTOaugust15
VALUES ('3', '103', 'white', '11111111113'); INSERT INTOaugust15
VALUES ('4', '103', 'white', '11111111114'); INSERT INTOaugust15
VALUES ('5', '103', 'black', '11111111115'); INSERT INTOaugust15
VALUES ('6', '104', 'white', '11111111116'); INSERT INTOaugust15
VALUES ('7', '104', 'black', '11111111117');