1

I am using Access to deal with a table with two fields. The table looks like

Field1   Field2
  bob      john
  kate     jum
  tim      kim

I want to select both fields and output them in order, but I can't use

Select Field1,Field2
From table1
order by Field1,Field2;

Because I want to sort them by the result of concatenation of Field1 and Filed2, but not order by Field1 first then Field2 secondly. What to do please? thanks.

2 Answers2

1
SELECT Field1 FROM table1
UNION
SELECT Field2 FROM table1

use UNION ALL if you want to keep duplicate names

Barry
  • 3,683
  • 1
  • 18
  • 25
  • Assuming that's their intent, you're not sorting the results – LittleBobbyTables - Au Revoir Oct 03 '14 at 15:56
  • Are you sure about that? http://stackoverflow.com/questions/421049/sql-server-union-what-is-the-default-order-by-behaviour – LittleBobbyTables - Au Revoir Oct 03 '14 at 15:59
  • When you do this for one field, it will be sorted as expected. [Check this](http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_union). With multiple fields I do not have a clue :). – Barry Oct 03 '14 at 16:03
  • SO disagrees: http://stackoverflow.com/questions/15766359/does-union-all-guarantee-the-order-of-the-result-set – LittleBobbyTables - Au Revoir Oct 03 '14 at 16:06
  • I can only speak from experience. Never had a problem (Oracle, SQL Server, MySql and Access). But there is a first time for everything. But I think he wants to display the two fields, both ordered. By chance they are already sorted in his example. I expect that is his expected output. – Barry Oct 03 '14 at 16:10
  • Thanks a lot for this. It works. Using union automatically sort the fields. – user3684119 Oct 03 '14 at 16:22
0

I would add an autonumbered field to the table. Then you can order by that field.

Mr. Mascaro
  • 2,693
  • 1
  • 11
  • 18