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

前端 未结 5 1252
眼角桃花
眼角桃花 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:32

    Modifying Tomalak's query slightly so that it will use indexes if they are present. Although unless there is an index on each field, a full table scan will happen anyway.

    SELECT
    *, 
    (
        IF(name="john doe", 1, 0) +
        IF(phone = "8183321234", 1, 0) +
        IF(email = "johndoe@yahoo.com", 1, 0) +
        IF(address = "330 some lane", 1, 0) 
    ) as matchCount
    FROM my_table 
    WHERE 
        name = "john doe" OR 
        phone = "8183321234" OR 
        email = "johndoe@yahoo.com" OR 
        address = "330 some lane"
    HAVING matchCount >= 3
    

提交回复
热议问题