我讀this和this,但我需要做一個GROUP BY查詢來設置另一個表。更新MySQL表與另一個表的數據
logintable
是這樣的:
id | name | login_date
------------------------
1 | michael | 2013-01-04
2 | michael | 2013-01-08
3 | mary | 2013-01-11
4 | john | 2013-01-15
5 | michael | 2013-01-19
6 | mary | 2013-01-22
7 | john | 2013-01-26
我做出這樣的查詢:
SELECT * FROM logintable GROUP BY name ORDER BY id ASC
這給我的用戶首次登錄日期:
1 | michael | 2013-01-04
3 | mary | 2013-01-11
4 | john | 2013-01-15
我有另一個表名爲userstable
是這樣的:
id | name | last_seen_date | first_login_date
------------------------------------------------
1 | michael | 2013-02-02 |
2 | john | 2013-02-04 |
3 | mary | 2013-02-16 |
我需要更新userstable的first_login_date列,第一個結果。我怎樣才能做到這一點 ? (我有75K記錄logintable和10K記錄userstable)
你的意思是像INSERT ... SELECT,UPDATE除外?你應該更新一個JOIN或兩個。 –
這並不能保證給你第一個登錄日期。改用'MIN(login_date)'。 –