我有一个查询,结果如下:
date From ----------------------- 20 Airport 20 Vehicle 23 Person 24 Person 28 Person 28 Airport
我的期望:
date From ----------------------- 20 Received From 20 Airport 20 Vehicle 23 Received From 23 Person 24 Received From 24 Person 28 Received From 28 Person 28 Airport
如何自动添加“接收自”行?
最佳答案
首先为所有每个日期
列值添加接收自
,并将实际的表格行与此合并使用UNION ALL
。然后使用 CASE
表达式将结果排序为 Received From
值首先。
查询
select t.`date`, t.`From` from(
select `date`, `From` from `myTable`
union all
select distinct `date`, 'Received From' from `myTable`
) t
order by t.`date`,
case t.`From` when 'Received From' then 1 else 2 end, t.`From`;
关于mysql - SQL:如何在特定日期之前添加自定义行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46561980/