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

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

Here is what I imagine, what the query looks like, I just can't understand the syntax.

SELECT * 
  FROM (1,2,3,4) as temp_table 
 WHERE temp_table.id 
LEFT JOIN table ON id IS NULL 

Is there any way to do this? What is the name of such a request?

I want to pass id list to mysql and I want it to spit out an identifier that is not in the database yet?

+3
source share
5 answers

To confirm that I understood correctly, you want to transfer a list of numbers and see which of these numbers is missing in the existing table? In fact:

SELECT Item
FROM IDList I
    LEFT JOIN TABLE T ON I.Item=T.ID
WHERE T.ID IS NULL

, , " ", /,

SELECT Number
FROM (SELECT Number FROM Numbers WHERE Number IN (1,2,3,4)) I
    LEFT JOIN TABLE T ON I.Number=T.ID
WHERE T.ID IS NULL

SQL Injection, - . , "1,2,3,4" , , - , , . http://www.sqlteam.com/article/parsing-csv-values-into-multiple-rows

, / , , .

+1

:

   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

, :

+2
SELECT * FROM table where id NOT IN (1,2,3,4)
0
source

I would just do:

SELECT id
FROM table
WHERE id IN (1,2,3,4);

And then process the list of results by removing the "records to send" returned by the request from the list.

0
source

What about a subquery? It might work. If not, it can lead you in the right direction.

SELECT * FROM table WHERE id NOT IN (
        SELECT id FROM table WHERE 1
);
0
source

Source: https://habr.com/ru/post/1739902/


All Articles