Minus operator giving me erros in mysql

前端 未结 2 1512
死守一世寂寞
死守一世寂寞 2021-01-06 07:14

I have two queries.

First query returning 11 rows and second query returning 6 rows when i use the minus operator on them it should return 5 rows as far as my under

相关标签:
2条回答
  • 2021-01-06 07:23

    MySQL does not support EXCEPT or MINUS.

    You can use NOT EXISTS , OUTER JOIN ... NULL or NOT IN (be careful of NULLs) to do an anti semi join.

    See examples and performance comparisons here

    0 讨论(0)
  • 2021-01-06 07:39

    Using a "not in" or "not exists" to perform a "minus" query on very large data sets can result in extremely long query times. I came up with a method that mimics the set based operations performed by other databases (merge, sort, remove duplicates).

    select column1, count(*), min(setnum)
    from
    (
          select distinct column1, 1 as setnum
            from table1
       union all
          select distinct column1, 2 as setnum
            from table2
    ) as tbl_a
    group by column1  
    having count(*) = 1 and min(setnum) = 1
    

    The above select yields very good performance on large data sets vs the use of not exists or not in. Essentially, it is looking for rows that only exist in the first set and not in the second. I have used this quite often lately with very good success with respect to performance.

    0 讨论(0)
提交回复
热议问题