我有2個表SQL JOIN由兩個日期排序兩個表
主表
Id_post
Id_user_post
Post
Date_post
二次表
Id_mentioned
Id_user
Id_user_post
Id_post
Date_mentioned
我有例如13條記錄第一表和3個記錄第二個。
首先表中的記錄(13)
1 herman this is text 1 10:00:00 15/09/2016
2 jhon this is text 2 11:00:00 15/09/2016
3 carl this is text 3 12:00:00 15/09/2016
4 herman this is text 4 13:00:00 15/09/2016
5 herman this is text 5 14:00:00 15/09/2016
6 herman this is text 6 15:00:00 15/09/2016
7 jhon this is text 7 16:00:00 15/09/2016
8 herman this is text 8 17:00:00 15/09/2016
9 herman this is text 9 18:00:00 15/09/2016
10 carl this is text 10 19:00:00 15/09/2016
11 herman this is text 11 20:00:00 15/09/2016
12 carl this is text 12 21:00:00 15/09/2016
13 herman this is text 13 22:00:00 15/09/2016
二表中的記錄
1 herman jhon 7 11:20:00 15/09/2016
2 jhon carl 10 12:30:00 15/09/2016
3 herman carl 3 14:50:00 15/09/2016
如果我選擇赫爾曼後,我想接下來的結果,按日期排序(發佈日期和日期提到)
1 herman this is text 1 10:00:00 15/09/2016
7 jhon this is text 7 11:20:00 15/09/20167 (date mentioned)
4 herman this is text 4 13:00:00 15/09/2016
5 herman this is text 5 14:00:00 15/09/2016
3 carl this is text 3 14:50:00 15/09/2016 (date mentioned)
6 herman this is text 6 15:00:00 15/09/2016
8 herman this is text 8 17:00:00 15/09/2016
9 herman this is text 9 18:00:00 15/09/2016
11 herman this is text 11 20:00:00 15/09/2016
13 herman this is text 13 22:00:00 15/09/2016
在這些結果中出現hermman提交的帖子和第二個ta中提到的帖子BLE,通過date_mentioned(其somenthing像Twitter,其中在配置文件中,主要結果顯示在自己的職位,那不屬於同一業主retwitts)
我試過SQL連接左下令
Select * from $table_posts left join $table_mentions on $table_posts.id_user=$table_mentions.id_user order by date_post,date_mentioned
我也試過,但沒有...
SELECT * FROM $table_posts WHERE id_user_post=(SELECT id_user_post FROM $table_mentions WHERE id_user_post='$id_user') AND id_user_post='$id_user' ORDER BY date_post,date_mentioned DESC
您使用的是日期時間數據類型,對吧?見http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry
CURRENT_TIMESTAMP() – masterhoo
第二張桌子與第一張桌子有什麼關係? – apokryfos