我的数据库中有以下行。
profileID | startDate | endDate
-------------------|-------------------|--------------
Jr.software eng. |2012-07-01 |2030-01-01
..................|...................|..............
Eng |2013-03-28 |2013-03-28
..................|...................|..............
Sr.eng |2013-04-09 |2013-04-17
..................|...................|..............
CEO |2012-11-21 |
..................|...................|..............
上面的行存储在我的数据库中。我希望得到像这样的条件的结果。
1. If endDate is null then I get only it related startDate.
在上面的行中,我的预期结果是
profileID | startDate | endDate
-------------------|-------------------|--------------
CEO |2012-11-21 |
..................|...................|..............
但如果行是这样的那么
profileID | startDate | endDate
-------------------|-------------------|--------------
Jr.software eng. |2012-07-01 |2030-01-01
..................|...................|..............
Eng |2013-03-28 |2013-03-28
..................|...................|..............
Sr.eng |2013-04-09 |2013-04-17
然后我的预期结果是
profileID | startDate | endDate
-------------------|-------------------|--------------
Jr.software eng. |2012-07-01 |2030-01-01
..................|...................|..............
我需要一个mysql查询。
答案 0 :(得分:1)
Select profileId, Case When endate is NULL then startDate
Else Max(EndDate) end As `Date`
From tablename
答案 1 :(得分:0)
SELECT
l.profileID,
IF(l.endDate IS NULL,l.startDate,MAX(endDate)) AS Date
FROM mytable AS l
答案 2 :(得分:0)
我猜你在寻找类似的东西:
SELECT profileID,startDate,MAX(endDate) AS endDate
FROM table1
group by profileID