How to get all lines using duplicate value in IN

Asked

Viewed 37 times

0

How do I get all lines using IN(), and inside IN() have duplicate values

SELECT NOME FROM PESSOA WHERE ID IN (1,1,1,2,3,3,4)

I want in return come 3 times the name of ID 1, and 2 times the name of ID 3.

  • You can’t use the IN for this. You’ll have to assemble a query more complex.

  • @bigown, have any idea how to accomplish this select?

1 answer

1


I found a way, more complex, but I think it’s the way

SELECT NOME FROM PESSOA WHERE ID = 1
UNION ALL
SELECT NOME FROM PESSOA WHERE ID = 1
UNION ALL
SELECT NOME FROM PESSOA WHERE ID = 1
UNION ALL
SELECT NOME FROM PESSOA WHERE ID = 2
UNION ALL
SELECT NOME FROM PESSOA WHERE ID = 3
UNION ALL
SELECT NOME FROM PESSOA WHERE ID = 3
UNION ALL
SELECT NOME FROM PESSOA WHERE ID = 4

I can make a script to generate SQL based on the values of IN then it won’t be too hard, just really hard...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.