mysql基于订单分组记录

时间:2017-05-23 16:01:34

标签: mysql

我在contact_details mysql表下创建了以下记录。

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

如何以下面的格式获取记录?

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

1 个答案:

答案 0 :(得分:2)

如果data_order总是001002,那么您可以使用类似的内容。

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'