我有這樣的兩個表訂單由兩個表日採用聯盟的替代
評論
| ID | COMMENT | FULLNAME | PHOTO_ID | CREATED_AT |
|----|---------|----------------|----------|---------------------------------|
| 76 | comment1| John Smith | 14 | February, 25 2014 10:38:02+0000 |
| 77 | comment2| James Anderson | 14 | February, 25 2014 10:38:14+0000 |
| 78 | comment3| Will Smith | 15 | March, 11 2014 14:24:08+0000 |
意見
| ID | USER | PID | CREATED_AT |
|----|------------|---------------------------------|
| 63 | user1| 15 | February, 25 2014 11:12:03+0000 |
| 64 | user1| 14 | February, 25 2014 11:14:10+0000 |
| 66 | user2| 15 | February, 25 2014 11:15:16+0000 |
| 68 | user2| 14 | February, 25 2014 10:38:12+0000 |
我想從comments.comment
獲取值,comments.fullname
,views.user
所以我可以將它們用於foreach,結果將按照如下順序排序:
John Smith added a comment February, 25 2014 10:38:12 //comment1 user2 viewed the file February, 25 2014 10:38:12 //view James Anderson added a comment February, 25 2014 10:38:12 //comment2
我使用這個查詢使用UNION,但我不認爲這是正確的做法,因爲我不能使用全名告訴是誰寫的評論或用戶查看文件
SELECT comment, fullname, comments.created_at
FROM comments
WHERE comments.photo_id = 14
UNION
SELECT user, '', views.created_at
FROM views
WHERE views.pid = 14
ORDER BY created_at DESC
,其結果是
| COMMENT | FULLNAME | CREATED_AT |
|----------|----------------|---------------------------------|
| user1 | | February, 25 2014 11:14:10+0000 |
| comment2 | James Anderson | February, 25 2014 10:38:14+0000 |
| user2 | | February, 25 2014 10:38:12+0000 |
| comment1 | John Smith | February, 25 2014 10:38:02+0000 |
所以,是有另一種方式這兩個表合併,按日期排序他們並分別得到了行? 這是sqlfiddle。
這些拖車桌之間的關係是什麼? – KrazzyNefarious
它們之間沒有關係。 – gael