2016-03-08 48 views
2

如何從下表中提取FIRST'StartAddress'的每一天。MYSQL:如何從表中提取以下數據?

結果必須是這樣的。

+------------+---------------+--------------------+--------------------+ 
| Date  | SamplingStart | StartAddress  | StopAddress  | 
+------------+---------------+--------------------+--------------------+ 
| 2016-02-22 | 08:00:00  | 26 Riverside Drive | 70 Piet Retief  | 
| 2016-02-23 | 05:15:00  | Balizza   | Mars    | 
+------------+---------------+--------------------+--------------------| 

表:

+------------+---------------+--------------------+--------------------+ 
| Date  | SamplingStart | StartAddress  | StopAddress  | 
+------------+---------------+--------------------+--------------------+ 
| 2016-02-22 | 08:00:00  | 26 Riverside Drive | 70 Piet Retief  | 
| 2016-02-22 | 09:00:00  | 10 Glen Drive  | 16 Olga Street  | 
| 2016-02-22 | 10:00:00  | 66 Bremerhof  | 26 Bluewater Drive | 
| 2016-02-22 | 11:00:00  | 7 New Street  | Walker Drive  | 
| 2016-02-22 | 13:00:00  | Goodwood Street | Blouberg   | 
| 2016-02-22 | 15:00:00  | Marine Drive  | William Moffet  | 
| 2016-02-22 | 18:00:00  | Jumanji   | Paterson   | 
| 2016-02-23 | 07:00:00  | 25 Eazi Street  | 70 Kings Street | 
| 2016-02-23 | 07:20:00  | Cool Blue   | Pick n Pay   | 
| 2016-02-23 | 05:15:00  | Balizza   | Mars    | 
| 2016-02-23 | 20:15:00  | Neverland   | Club Shakes  | 
| 2016-02-23 | 23:15:00  | Castros   | Stones    | 
| 2016-02-23 | 14:45:00  | Seagate   | Ermelo    | 
| 2016-02-23 | 16:30:00  | Mafikeng   | Goodwood   | 
+------------+---------------+--------------------+--------------------+ 
+0

使用分組日期列。 –

回答

1

您可以使用NOT EXISTS()

​​
1

您可以通過添加行號爲每個日期達致這。

查詢

select t.`Date`, t.SamplingStart, t.StartAddress, t.StopAddress from 
(
    select `Date`,SamplingStart, StartAddress, StopAddress, 
    (
     case `Date` when @curA 
     then @curRow := @curRow + 1 
     else @curRow := 1 and @curA := `Date` end 
    ) as rn 
    from your_table_name t, 
    (select @curRow := 0, @curA := '') r 
    order by `Date`, SamplingStart 
)t 
where t.rn = 1; 

SQL Fiddle demo

2

試試這個:

SELECT 
    * 
FROM 
    tbl 
WHERE 
    (Date, SamplingStart) IN 
    (
     SELECT tbl2.Date, MIN(tbl2.SamplingStart) 
     FROM tbl tbl2 
     WHERE tbl.Date = tbl2.Date 
    ); 
1
Select Date, MIN(SamplingStart) AS SamplingStart, StartAddress, StopAddress 
from Table Group By Date; 
+0

歡迎,並感謝您的回答。請始終解釋僅用於代碼的答案。你改變了什麼,有什麼影響?謝謝。 –

相關問題