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

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

    SELECT
      * 
    FROM 
      my_table 
    WHERE 
      CASE WHEN name = "john doe"           THEN 1 ELSE 0 END +
      CASE WHEN phone = "8183321234"        THEN 1 ELSE 0 END +
      CASE WHEN email = "johndoe@yahoo.com" THEN 1 ELSE 0 END +
      CASE WHEN address = "330 some lane"   THEN 1 ELSE 0 END
      >= 3;
    

    Side note: this will very likely not be using indexes efficiently. On the other hand, there will very likely be no indexes on these kinds of columns anyway.

提交回复
热议问题