简体   繁体   中英

mysql grouping of records based on order

I have the following records created under contact_details mysql table.

+-----------+-------------+------------+
|data_ref_id| data_value  | data_order |
+-----------+-------------+------------+
|ref001     | Alex        |        001 |
|ref001     | 040-345-234 |        002 |
|ref002     | Adam        |        001 |
|ref002     | 040-225-254 |        002 |
+-----------+-------------+------------+

How to fetch records in below format?

+------+-------------+
| Alex | 040-345-234 |
| Adam | 040-225-254 |
+------+-------------+

If data_order is always 001 and 002 then you can use something like this.

select t1.data_value as name,t2.data_value as phone
from your_table t1
inner join your_table t2
on t1.data_ref_id=t2.data_ref_id
and t1.data_order='001'
and t2.data_order='002'

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