MYSQL Select within same table
I have a table that I need a MYSQL Select statement so I can find the corrupt data and manually fix it.
Sample table
TABLE_A id | type | value1 | value2 1 | 10 | 123 | 987 2 | 10 | 123 | 987 3 | 10 | 123 | 789 4 | 20 | 123 | 987 5 | 20 | 456 | 987 6 | 30 | 123 | null 7 | 30 | 123 | null 8 | 40 | 123 | 987
I need a select statement that will list records that if they have the same "typ开发者_C百科e" and value1 is not the same and/or value2 not the same.
For Example
ID 1,2,3 - will be displayed because value2 is different in id 3 and they have the same "type"
ID 4,5 will be displayed because value1 is different and they have the same "type"
ID 6,7 will NOT be displayed because value1 and value2 are the same for the same "type"
ID 8 will NOT be displayed because there is only one with this type.
I have been trying to get my head around this for days and need some help. Thanks
SELECT a.id as problem_id
FROM TABLE_A as a JOIN TABLE_A as b
ON a.type = b.type
WHERE a.value1 <> b.value1 OR a.value2 <> b.value2
GROUP BY problem_id;
Try with:
SELECT * FROM TABLE_A WHERE type IN (SELECT type FROM TABLE_A GROUP BY `type`, `value1`, `value2` HAVING count(value1) > 1 OR count(value2) > 1)
精彩评论