0

I have a list of integers that contains duplicate values, such as 1, 2, 2, 3, 3, 4, 5, 6. Is there a way, using MySQL, to SELECT a list of unique values (1, 2, 3, 4, 5, 6 in this case)?

I realize it can be done by creating and inserting into a temporary table:

BEGIN;
CREATE TEMPORARY TABLE temp (value INT);
INSERT INTO temp VALUES (1), (2), (2), (3), (3), (4), (5), (6);
SELECT GROUP_CONCAT(DISTINCT value) FROM temp;
DROP TEMPORARY TABLE temp;
ROLLBACK;

but is there a way that does not require a temporary table?

The list of integers is not coming from another MySQL table; pretend it is hard-coded.

Jimmy P
  • 1,790
  • 4
  • 17
  • 32
  • You use simply distinct in select – Rams Apr 11 '17 at 18:26
  • is the initial *list of integers* just a string? – RomanPerekhrest Apr 11 '17 at 18:27
  • @RomanPerekhrest Yeah – Jimmy P Apr 11 '17 at 18:27
  • Does a single field has "1, 2, 2, 3, 3, 4, 5, 6" or these are in separate rows? If there are separate rows, used DISTINCT. If its a single row then let us know, we will try to solve it –  Apr 11 '17 at 18:28
  • Is this data, the comma delimited string of integers, already in a field in mysql, or is your plan to send mysql the string and have it return back distinct? – JNevill Apr 11 '17 at 18:31
  • @JNevill The latter – Jimmy P Apr 11 '17 at 18:32
  • 1
    This feels very much like an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). IF the data set is very large, which has led you to using mysql to solve it, then a temp table seems perfectly reasonable. If the data is small, then perhaps mysql isn't the best option for whatever platform you are sending this from. – JNevill Apr 11 '17 at 18:33
  • Use this solution http://stackoverflow.com/questions/17942508/sql-split-values-to-multiple-rows applying a distinct – Jorge Campos Apr 11 '17 at 18:33
  • @JorgeCampos I'm afraid I don't see the correlation. Could you please elaborate? – Jimmy P Apr 11 '17 at 18:38
  • "split values to multiple rows" you have a list of values (in your case will not be a column but a fixed value) and you want to select those values as rows distinct. The link I provided do exactly that only thing you need is to add the distinct after the split. – Jorge Campos Apr 11 '17 at 18:40

0 Answers0