我需要写一个查询,我甚至不知道从哪一个开始。我有一组我没有创建并且无法更改的表。
member table
+----+-------+
| id | class |
+----+-------+
| 1 | 1 |
| 2 | 2 |
+----+-------+
member_data table
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 1 | John | Jones |
| 2 | Juan | Jones |
+----+------------+-----------+
member_entries table
+----+-----------+-------+------------+
| id | member_id | title | date |
+----+-----------+-------+------------+
| 1 | 1 | test1 | 1265042580 |
| 2 | 1 | test2 | 1265042581 |
| 3 | 2 | test3 | 1265042582 |
| 4 | 3 | test4 | 1265042583 |
+----+-----------+-------+------------+
我需要从member_data中选择id,first_name,从member_entries中选择最新的标题,以及与member_id匹配的member_entries的总数。我需要能够在第一个表中选择成员的类是特定数字的位置。我还需要能够通过任何这些列进行排序。如果我选择了成员类为1的成员,那么这就是我需要输出的内容。
+----+------------+-------+------------+
| id | first_name | title | date |
+----+------------+-------+------------+
| 1 | John | test2 | 1265042581 |
+----+------------+-------+------------+
答案 0 :(得分:1)
SELECT member.id, member_data.first_name, member_entries.title, member_entries.date
FROM member
INNER JOIN member_data ON member.id = member_data.member_id
INNER JOIN member_entries ON member.id = member_entries.member_id
WHERE member.class = 1
ORDER BY member_entries.date DESC
LIMIT 1
并根据需要构建WHERE
和ORDER BY
子句。
答案 1 :(得分:1)
SELECT m.id, md.first_name, me.title, me.date
FROM member m
INNER JOIN member_data md ON md.id = m.id
INNER JOIN member_entries me on me.id = m.id
WHERE me.date =
(SELECT MAX(date) FROM member_entries WHERE id = m.id)
AND m.class = 1