Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL to search and delete with several keycolumns

Tags:

sql

sql-server

A bit bad title maybe but I try to explain what I mean.

I use SQL like this to build a list

SELECT id 
FROM parcel 
WHERE id IN (113715, 113824, 113855, 113954, 114010, 114116, 114272, 114329)

where ID is a column in parcel table that is quarantined to be unique, very straightforward.

But some tables use many columns to be unique.

SELECT id1, id2 
FROM trip 
WHERE id1, id2 IN ((113715, 113824), (113855, 113954), (114010, 114116),(114272, 114329))

The last SQL is obviously not working.

I want to select rows where

id1 = 113715 and id2=113824 or
id1 = 113855 and id2=113954 or
id1 = 114010 and id2=114116 or
id1 = 114272 and id2=114329

In reality the generated SQL can contain 500 - 1000 ids.

What SQL should I use ?

EDIT

This is a generated SQL. The ID's come from another database on another server so JOIN for example is not possible.

like image 924
Roland Bengtsson Avatar asked Dec 10 '22 13:12

Roland Bengtsson


1 Answers

SELECT id1, id2 FROM trip 
INTERSECT
select id1, id2 from (values (113715,113824),(113855,113954),(114010,114116),(114272,114329)) a(id1,id2)

USE INTERSECT

like image 167
Akshey Bhat Avatar answered Dec 22 '22 18:12

Akshey Bhat