MYSQL: Can you pull results that match like 3 out of 4 expressions?

前端 未结 5 1189
眼角桃花
眼角桃花 2021-02-08 11:50

Say I have a query like this:

SELECT * FROM my_table WHERE name = \"john doe\" AND phone = \"8183321234\" AND email = \"johndoe@yahoo.com\" AND address = \"330 s         


        
5条回答
  •  臣服心动
    2021-02-08 12:31

    Same thing using indexes:

    SELECT  *
    FROM    (
            SELECT  id
            FROM    (
                    SELECT  id
                    FROM    mytable _name
                    WHERE   name = 'john doe'
                    UNION ALL
                    SELECT  id
                    FROM    mytable _name
                    WHERE   phone = '8183321234'
                    UNION ALL
                    SELECT  id
                    FROM    mytable _name
                    WHERE   email = "johndoe@yahoo.com"
                    UNION ALL
                    SELECT  id
                    FROM    mytable _name
                    WHERE   address = '330 some lane'
                    ) q
            GROUP BY 
                    id
            HAVING
                    COUNT(*) >= 3
            ) di, mytable t
    WHERE   t.id = di.id
    

    See the entry in my blog for performance details.

提交回复
热议问题