简体   繁体   中英

Make a common column value as the heading of all results in mysql select statement

I want to retrieve data from mysql database, but all i need is to make the common value of the data as the heading of all the retrieved data example of the table:
ID | Name | orderDate |
1 | John | 10-07-2019 |
2 | Maige | 10-07-2019 |
3 | Allyc | 10-07-2019 |
4 | Diana | 11-07-2019 |
5 | Selen | 11-07-2019 |
6 | Jacky | 12-07-2019 |

So I want my results to appear like this

10 - 07- 2019
1. John
2. Maige
3. Allyc

11 - 07- 2019
4. Diana
5. Selen

12 - 07- 2019
6. Jacky

Thanks in advance...

You should do this in the front end, not the db-in OO programming terms, think of rows as instances of an object. Creating fake objects of a completely different type and inserting them into the list for purely presentational reasons is an ugly hack..

SELECT sho FROM
(
  SELECT DISTINCT 0 as ord, orderdate, DATE_FORMAT(orderdate, '%d - %m - %y') as sho FROM orders
  UNION ALL
  SELECT id as ord, orderdate, CONCAT(id, '. ', name) as sho FROM orders
) x
ORDER BY orderdate, ord

Ugh. So we generate some fake rows with s formatted date, the date (as a date) and a 0 (lower than an id) and union them with the real data date, id and s formatted text. When this lot of data (fake plus real) is sorted by date then ord the fake rows that form the "header" come out first, then the data, then the next header

Please do this on the front end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM