Alternative to Intersect in MySQL

前端 未结 8 1555
暖寄归人
暖寄归人 2020-11-22 11:53

I need to implement the following query in MySQL.

(select * from emovis_reporting where (id=3 and cut_name= \'全プロセス\' and cut_name=\'恐慌\') ) 
intersect
( sel         


        
相关标签:
8条回答
  • 2020-11-22 12:16

    AFAIR, MySQL implements INTERSECT through INNER JOIN.

    0 讨论(0)
  • 2020-11-22 12:17

    Your query would always return an empty recordset since cut_name= '全プロセス' and cut_name='恐慌' will never evaluate to true.

    In general, INTERSECT in MySQL should be emulated like this:

    SELECT  *
    FROM    mytable m
    WHERE   EXISTS
            (
            SELECT  NULL
            FROM    othertable o
            WHERE   (o.col1 = m.col1 OR (m.col1 IS NULL AND o.col1 IS NULL))
                    AND (o.col2 = m.col2 OR (m.col2 IS NULL AND o.col2 IS NULL))
                    AND (o.col3 = m.col3 OR (m.col3 IS NULL AND o.col3 IS NULL))
            )
    

    If both your tables have columns marked as NOT NULL, you can omit the IS NULL parts and rewrite the query with a slightly more efficient IN:

    SELECT  *
    FROM    mytable m
    WHERE   (col1, col2, col3) IN
            (
            SELECT  col1, col2, col3
            FROM    othertable o
            )
    
    0 讨论(0)
提交回复
热议问题