skip to Main Content

If I do:

SELECT a, a FROM myTable

I can get back the "a" value two times, and that’s fine, it works.
But I want to do something like:

SELECT DISTINCT(a) AS a1, DISTINCT(a) AS a2 FROM myTable

Of course I do know and do understand why you cannot use DISTINCT twice. And in fact I do not want it 🙂 More than that, if possible I want to ‘optimize’ it somehow, since it’s already been calculated. I tried:

SELECT DISTINCT(a) AS a1, a1 FROM myTable

but still no luck…

Example, if the table contains

aaa
bbb
bbb
bbb
ccc
aaa

I want the output to be

aaa | aaa
bbb | bbb
ccc | ccc

PS: The "why" is because I need the output of the query to be passed to a function that needs those parameters "duplicated" in that order, and while I theoretically could edit and duplicate those values manually before calling the function, I would rather let MySQL give me back the correct string.

2

Answers


  1. DISTINCT always applies to the entire row of your result set. You cannot make a single column distinct. Just use

    select distinct a, a from table
    
    Login or Signup to reply.
  2. use DISTINCT keyword to avoid getting same value twice.

    select Distinct a, a from table_Name

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search