Alternative to Intersect in MySQL

前端 未结 8 1777
暖寄归人
暖寄归人 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:15

    For completeness here is another method for emulating INTERSECT. Note that the IN (SELECT ...) form suggested in other answers is generally more efficient.

    Generally for a table called mytable with a primary key called id:

    SELECT id
    FROM mytable AS a
    INNER JOIN mytable AS b ON a.id = b.id
    WHERE
    (a.col1 = "someval")
    AND
    (b.col1 = "someotherval")
    

    (Note that if you use SELECT * with this query you will get twice as many columns as are defined in mytable, this is because INNER JOIN generates a Cartesian product)

    The INNER JOIN here generates every permutation of row-pairs from your table. That means every combination of rows is generated, in every possible order. The WHERE clause then filters the a side of the pair, then the b side. The result is that only rows which satisfy both conditions are returned, just like intersection two queries would do.

提交回复
热议问题