0

Reading this answer to a SO question on how to append a row onto a MySQL result, we can do the following to append the value Jason to the names of Customers:

Select NAME
From Customers
UNION ALL
Select 'Jason'

So we end up with:

NAME
Actual Customer 1
Actual Customer 2
Jason

But how could we add Jason to the beginning of the results so what we have:

NAME
Jason
Actual Customer 1
Actual Customer 2

(Would like to do this without using Order By)

Community
  • 1
  • 1
Craig van Tonder
  • 7,497
  • 18
  • 64
  • 109
  • 1
    `Select 'Jason' as Name UNION ALL Select Name From Customers` – juergen d Oct 25 '16 at 10:21
  • "Would like to do this without using Order By" - why? – onedaywhen Oct 25 '16 at 10:21
  • @onedaywhen Because I am actually doing this with dates and not names, sorting does not work well in this case and I get an unwanted result. – Craig van Tonder Oct 25 '16 at 10:22
  • Well in that case it would probably have been sensible to use dates in your question. _Wouldn't it?_ If I add 2 apples to 3 apples, what do I get? _Well it definitely is not ORANGES now is it?_ – RiggsFolly Oct 25 '16 at 10:23
  • @RiggsFolly Might have but in my opinion it makes it more complicated and besides the point to say that I want to append a string value to a list of dates. – Craig van Tonder Oct 25 '16 at 10:25
  • And if you use ORDER BY then the result will not guarantee to have `JasonDATE` at the top will it – RiggsFolly Oct 25 '16 at 10:25
  • @RiggsFolly I guess it is specific to my use case, I simply want to output the result once via the MySQL query. I was only suggesting that I do not want to use Order By to achieve this end result. It's a good point that you have though. – Craig van Tonder Oct 25 '16 at 10:27
  • If you omit `ORDER BY` then the order is not guaranteed. Before accepting an answer, be sure you know **why you are sure** you will get the order you require :) – onedaywhen Oct 25 '16 at 10:28
  • @juergend Thanks, that was spot on. – Craig van Tonder Oct 25 '16 at 10:30
  • @onedaywhen How is the order not guaranteed here? In my mind it is saying take {this} result set and then add {that} result set onto it? Is that not correct? – Craig van Tonder Oct 25 '16 at 10:31

1 Answers1

1

Have you tried this

Select 'Jason' As `Name`
UNION ALL
Select Name
From Customers

If you want the second query ordered then use something like

Select 'Jason' As `Name`
UNION ALL
(Select `myDate`
From Customers
Order By `myDate`
)
PaulF
  • 6,673
  • 2
  • 18
  • 29