-6

I have 3 tables

Table 1 (tickets):

id  | name
---------------
1   | Ticket 1
2   | Ticket 2
3   | Ticket 3

Table 2 (ticket_field):

id | name
------------
1  | Field 1
2  | Field 2
3  | Field 3

Table 3 (ticket_field_map):

id | ticket_id | field_id | value
------------------------------------
1  |     1     |     1    | Value 1
2  |     1     |     2    | Value 2
3  |     1     |     3    | Value 3
4  |     2     |     1    | Value 4
5  |     2     |     2    | Value 5
6  |     2     |     3    | Value 6
7  |     3     |     1    | Value 7
8  |     3     |     2    | Value 8
9  |     3     |     3    | Value 9

The final Table on which sorting is to be done:

ticket_id | ticket_name | Field 1 | Field 2 | Field 3
-----------------------------------------------------
     1    |   Ticket 1  | Value 1 | Value 2 | Value 3
     2    |   Ticket 2  | Value 4 | Value 5 | Value 6
     3    |   Ticket 3  | Value 7 | Value 8 | Value 9

On this table, I have to give the user option to sort on each field value.

  • 1
    If you asked any search engine, you would probably find this duplicate question [PHP MySQL Order by Two Columns](https://stackoverflow.com/questions/514943/php-mysql-order-by-two-columns) – Hamza Abdaoui Mar 16 '18 at 08:05

2 Answers2

2

I think a simple two step ordering should work:

SELECT *
FROM yourTable
ORDER BY
    `key`, value;

By the way, KEY is a reserved MySQL keyword, and you should avoid naming your columns and tables using it.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Use Order by key asc or desc whatever order you want

Ketan G
  • 507
  • 1
  • 5
  • 21