2013-07-13 43 views
1

一個名爲users_log的表包含用戶登錄和註銷時間和日期,但我無法按照我的意圖對數據進行排序。如何檢索相同的列數據並顯示爲組

查詢

mysql_query('SELECT * FROM users_log WHERE u_id = 52 ORDER BY date_log') 

它像

Fri 12 Jul 13 - 03:41:40 PM - 03:42:31 PM 

Fri 12 Jul 13 - 05:35:40 PM - 05:42:31 PM 

Sat 13 Jul 13 - 02:29:09 PM - 02:34:52 PM 

users_log表數據進行排序

log_id, u_id, date_log, logged_date, login_time, logout_time 

2,  52, 2013-01-12, Fri 12 Jul 13, 03:41:40 PM, 03:42:31 PM 

我打算顯示的數據

  Fri 12 Jul 13 
    03:41:40 PM - 03:42:31 PM 
    05:35:40 PM - 05:42:31 PM 
     Sat 13 Jul 13 
    02:29:09 PM - 02:34:52 PM 

回答

3

這樣做:

$result = mysql_query('SELECT * FROM users_log WHERE u_id = 52 ORDER BY date_log'); 
$sorting = array(); 
while($row = mysql_fetch_assoc($result)) 
{ 
    $sorting[$row['logged_date']][] = $row['login_time']."-".$row['logout_time']; 
} 

//Now you loop over the sorted data using foreach($sorting as $key => $value) and display it any way you want. 

我希望這能有一定的幫助。

+0

謝謝saran banarjee ...它顯示爲我想要的,但它顯示未定義的偏移量1 –

+0

讓我看看代碼foreach中的行($ sorting爲$ key => $ value)您已完成 –

1

恕我直言,你應該這樣做(我假設你想要做它在PHP):

SELECT date_log FROM users_log WHERE u_id = 52 GROUP BY date_log, 

然後遍歷結果(在PHP中)並獲得日期對應的行:

SELECT * FROM users_log WHERE u_id = 52 AND date_log = $date_for_current_iteration ORDER BY date_log; 
相關問題