0
我在MySQL表中有这个数据。我的病情需要顺序结果:MySQL - 按条件排列两列
- 如果
effective_to
是NULL
,以显示第一和order by effective_from DESC
- 如果
effective_to
是NOT NULL
,order by effective_to DESC
我的数据
name effective_from effective_to
person 01 1999-04-01 1999-05-31
person 02 1999-04-01 2000-07-06
person 03 1999-04-01 2000-09-25
person 04 1999-04-01 2000-09-25
person 07 1999-04-01 2000-09-25
person 05 2013-04-29 NULL
person 08 2010-06-17 2012-09-27
person 09 2010-12-02 2012-09-27
person 10 2017-02-10 NULL
person 11 2017-02-10 NULL
person 12 1999-04-01 2000-07-06
person 13 2011-04-28 2015-10-06
person 05 2013-04-29 2017-02-15
person 06 2015-09-22 2017-02-15
person 06 2015-09-22 2017-02-10
我需要此结果订单
name effective_from effective_to
person 11 2017-02-10 NULL
person 10 2017-02-10 NULL
person 05 2013-04-29 NULL
person 06 2015-09-22 2017-02-15
person 05 2013-04-29 2017-02-15
person 06 2015-09-22 2017-02-10
person 13 2011-04-28 2015-10-06
person 09 2010-12-02 2012-09-27
person 08 2010-06-17 2012-09-27
person 07 1999-04-01 2000-09-25
person 03 1999-04-01 2000-09-25
person 04 1999-04-01 2000-09-25
person 12 1999-04-01 2000-07-06
person 02 1999-04-01 2000-07-06
person 01 1999-04-01 1999-05-31
我该怎么做?
使用的是选择不同的'effective_to',每个使用所需'ORDER BY'两个查询的'UNION'。 – Barmar
SO不是免费的编码服务。你必须尝试自己解决问题。如果无法正常工作,请发布您尝试的内容,我们会帮助您解决问题。 – Barmar