SQL select from data in query where this data is not already in the database?

前端 未结 5 1199
日久生厌
日久生厌 2021-01-20 22:00

I want to check my database for records that I already have recorded before making a web service call.

Here is what I imagine the query to look like, I just can\'t

5条回答
  •  不要未来只要你来
    2021-01-20 22:36

    Use:

       SELECT x.id
         FROM (SELECT @param_1 AS id
                 FROM DUAL
               UNION ALL
               SELECT @param_2
                 FROM DUAL
               UNION ALL
               SELECT @param_3
                 FROM DUAL
               UNION ALL
               SELECT @param_4
                 FROM DUAL) x
    LEFT JOIN TABLE t ON t.id = x.id
        WHERE x.id IS NULL
    

    If you need to support a varying number of parameters, you can either use:

    • a temporary table to populate & join to
    • MySQL's Prepared Statements to dynamically construct the UNION ALL statement

提交回复
热议问题