2012-01-13 49 views
0

我想從另一個表中顯示最近的評論和日期。不同的表格被鍵入主表中的行。換言之:從另一個表中選擇最近的一行

「主」表被鍵入「筆記」表。 「筆記」表可以包含任意數量的條目,提供參考「主」錶行所採取的步驟的詳細信息,但我只想在查詢中顯示最新的評論和日期。

主表名是「表」「註釋」的「主」鍵。 「筆記」中我需要的2列是「entry_timestamp」和「activity_notes」。

+0

你提到「不同」,「進步」,「主」和「注意事項」表 - 請你澄清表的架構? – simchona 2012-01-13 00:08:12

+0

我編輯了「進度」。現在只給出真實姓名並且表名用引號引起來。抱歉。 – 2012-01-13 00:24:51

+0

你可能會給你的表的架構?從你的問題看來,你只是想從'notes'和它對應的'activity_notes'中找到最近的'entry_timestamp',也就是'notes'。在這種情況下,「主」表是不相關的? – 2012-01-13 02:17:02

回答

1

喜歡的東西

select 
    (select top 1 activity_notes from notes n where n.mainId = m.id order by m.entry_timestamp desc) 
    , (select top 1 entry_timestamp from notes n where n.mainId = m.id order by m.entry_timestamp desc) 
from main m 
+0

mysql> select(從備註n中選擇top 1 activity_notes,其中n.mainId = m.id order by m.entry_timestamp desc),(從備註n中選擇top 1 entry_timestamp,其中n.mainId = m.id order by m.entry_timestamp desc )來自主米; 錯誤1064(42000):您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在靠近'1 notes_n的activity_notes'處使用正確的語法,其中第1行的n.mainId = m.id order by m.entry_timestamp d' – 2012-01-13 00:25:52

+0

MySQL沒有'TOP n'。你必須使用等效的'LIMIT n' – 2012-01-13 00:30:10

+0

@ypercube:我改變了它,就像你說的,並得到這個:'mysql> select(從notesn選擇LIMIT 1 activity_notes,其中n.mainId = m.id order by m.entry_timestamp desc ),(從筆記n中選擇LIMIT 1 entry_timestamp,其中n.mainId = m.id order by m.entry_timestamp desc)from main m - >; 錯誤1064(42000):您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第1行的'LIMIT 1 activity_notes from notes n'附近使用,其中'n.mainId = m.id order by m.entry_times'在第1行 mysql>' – 2012-01-13 00:51:20

相關問題