How to compare two query results for equality in MySQL? How to compare two query results for equality in MySQL? database database

How to compare two query results for equality in MySQL?


SELECT  CASE WHEN count1 = count2 AND count1 = count3 THEN 'identical' ELSE 'mis-matched' ENDFROM(  SELECT    (SELECT COUNT(*) FROM <query1>) AS count1,    (SELECT COUNT(*) FROM <query2>) AS count2,    (SELECT COUNT(*) FROM (SELECT * FROM query1 UNION SELECT * FROM query2) AS unioned) AS count3)  AS counts


This would be a little easier if MySQL supported FULL OUTER JOIN also note that if the the two queries give the same results but in different order that will be deemed equivlant

SELECT   COUNT(*) FROM (    (SELECT A, b, c FROM A) a    LEFT OUTER JOIN       (SELECT A, b, c FROM b) B    ON A.a = b.a and a.b = b.b and a.c = b.cUNION     (SELECT A, b, c FROM A) a   RIGHT OUTER JOIN    (SELECT A, b, c FROM b) B    ON A.a = b.a and a.b = b.b and a.c = b.c) WHERE a.a is null or b.a is null

If the count =0 then the two queries are the same

Also because I'm using UNION duplicates are being removed. So there's a potential inaccuracy there.


You can't do MINUS in MySQL, so here's how to do it without:

select if(count(*)=0,'same','different') from (select col1,col2,col3from tableonewhere ( col1, col2, col3 ) not in( select col4, col5, col6  from tabletwo )unionselect col4, col5, col6from tabletwowhere ( col4, col5, col6 ) not in( select col1, col2, col3  from tableone )) minusintersec;

That's given:

tableone (col1 integer, col2 integer, col3 integer );tabletwo (col4 integer, col5 integer, col6 integer );