简体   繁体   English

SQL:检查哪个ID已经存在

[英]SQL: check to see which id's already exist

Let's say I have a list of IDs and I want to see if rows already exist with those IDs in my SQL database. 假设我有一个ID列表,我想看看我的SQL数据库中是否存在包含这些ID的行。 Is there a single SQL command that I can issue to get back the subset of the ID list that's not already represented in the database? 是否有一个SQL命令可以发回以获取尚未在数据库中表示的ID列表的子集? I could iterate over each ID in my code and issue a separate SQL check for each ID, but that sounds inefficient. 我可以迭代我的代码中的每个ID,并为每个ID发出单独的SQL检查,但这听起来效率低下。

I'm trying to do this against an sqlite database. 我正在尝试针对sqlite数据库执行此操作。 thanks! 谢谢!

Drop the IDs into a table and then try: 将ID放入表中,然后尝试:

SELECT C.id
FROM checkids C
LEFT JOIN mytable M
ON M.id = C.id
WHERE M.id IS NULL

You could use the IN operator; 你可以使用IN运算符; for instance, to test for ids 1, 2, 8, and 42, you'd do: 例如,要测试ID 1,2,8和42,您需要:

SELECT id
FROM tbl
WHERE id IN (1, 2, 8, 42);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM