sqlite,在连接表中按日期/整数排序

时间:2015-10-11 12:14:39

标签: sqlite join inner-join

我有两张桌子

姓名

id | name
---------
5  | bill
15 | bob
10 | nancy

条目

id | name_id | added    | description
----------------------------------
2  | 5       | 20140908 | i added this
4  | 5       | 20140910 | added later on
9  | 10      | 20140908 | i also added this
1  | 15      | 20140805 | added early on
6  | 5       | 20141015 | late to the party

我希望按Names表中数字最低的added值中的第一个来命令Entries,并显示由{命令排序的两个表中的行总体而言{1}}列,因此结果将类似于:

added

我查看了第一个项目的联接(例如SQL Server: How to Join to first row)但是无法让它工作。

任何提示?

2 个答案:

答案 0 :(得分:1)

尝试此查询:

SELECT Names.id, Names.name, Entries.added, Entries.description 
FROM Names 
INNER JOIN Entries 
ON Names.id = Entries.name_id 
ORDER BY Entries.added

如果您希望以相反的顺序添加DESC,例如:ORDER BY Entries.added DESC

答案 1 :(得分:0)

这应该这样做:

SELECT n.id, n.name, e.added, e.description 
FROM Names n INNER JOIN
     (SELECT name_id, description, Min(added) FROM Entries GROUP BY name_id, description) e
     ON n.id = e.name_id 
ORDER BY e.added