2

I realize most of the time you don't want this, but I'm looking for a way to get duplicate rows using IN.

SELECT `id`, `name`
FROM `users`
WHERE `id` IN
      (11, 11, 3, 11, 6)

My desired result:

+----+---------+
| id | name    |
+----+---------+
| 11 | Alice   |
| 11 | Alice   |
| 3  | Bob     |
| 11 | Alice   |
| 6  | Charlie |
+----+---------+

This is a really simplified example, but in the real query, being able to do this with IN would make my life about 1000 times easier.

1
  • 3
    If you tell us WHY you would want this to happen, I am sure someone would help you solve this in a better way Commented Oct 22, 2020 at 17:43

3 Answers 3

4

What you're looking for is a join, which produces the desired cartesian product for you:

SELECT users.id, users.name
FROM users
JOIN (
  SELECT 11 id, 1 ordinal UNION ALL
  SELECT 11 id, 2 ordinal UNION ALL
  SELECT 3  id, 3 ordinal UNION ALL
  SELECT 11 id, 4 ordinal UNION ALL
  SELECT 6  id, 5 ordinal
) t ON t.id = users.id
ORDER BY ordinal

Ordering is optional, of course... db-fiddle here

Sign up to request clarification or add additional context in comments.

2 Comments

This is admittedly unwieldy, but produces the right results. Thanks!
@JohnAlexander: Well, you don't have to inline the derived table in your query. You might be able to store the data in some (temporary) table. The join is the same.
3

No, it can't.

You're asking for all the rows, existing in a table, that meet some criteria (or, in this case, one criterion: the ID must be any of $thisListOfIds).

You're not asking to perform any duplication operations on the resulting set of rows.

More than that, I question whether this, even in your real-world complex use case, is actually the right way to go. You don't usually synthesize more redundant data when you perform queries, even for intermediary steps.

4 Comments

Picture a hierarchical list of users and subusers, several layers deep, where the same user can appear in multiple places in relation to multiple different other users.
That doesn't explain why you need to duplicate any rows. Usually you'd be trying to deduplicate in that scenario, not the other way around.
The required end result is a set with duplicate rows. Not sure why I would want to remove duplicates when the end goal is duplicates?
@JohnAlexander I'm saying, the requirement for that end result is suspicious and unusual. Usually one does the opposite. But that's fine, you do you :)
1

Same idea as @Lukas Eder with a bit more conscise syntax:

WITH t(id, ordinal) AS (
 VALUES ROW(11, 1), ROW(11,2), ROW(3,3), ROW(11,4), ROW(6,5)
)
SELECT users.id, users.name
FROM users
JOIN t ON t.id = users.id
ORDER BY ordinal;

db<>fiddle demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.