【发布时间】:2018-06-25 00:13:40
【问题描述】:
问题: 构造 SQL 语句以查找 Michael Phelps 发送的所有消息。注意:您必须使用 WHERE 子句来设置此查询的条件。显示以下列: - 发件人的名字 - 发件人的姓氏 - 收件人的名字 - 收货人的姓氏 - 消息 ID - 信息 - 消息时间戳
桌子:
SELECT * FROM contact_list;
+---------------+-----------+------------+----------+
| connection_id | person_id | contact_id | favorite |
+---------------+-----------+------------+----------+
| 1 | 1 | 2 | n |
| 2 | 1 | 3 | n |
| 3 | 1 | 4 | n |
| 4 | 1 | 5 | n |
| 5 | 1 | 6 | n |
| 6 | 2 | 1 | y |
| 7 | 2 | 3 | n |
| 8 | 2 | 4 | n |
| 9 | 3 | 1 | y |
| 10 | 3 | 4 | n |
| 11 | 4 | 5 | n |
| 12 | 4 | 6 | n |
| 13 | 5 | 1 | y |
| 14 | 5 | 6 | n |
| 15 | 7 | 1 | y |
| 16 | 7 | 2 | n |
| 17 | 7 | 4 | n |
+---------------+-----------+------------+----------+
17 rows in set (0.00 sec)
SELECT * FROM person;
+-----------+------------+-----------+------------+
| person_id | first_name | last_name | person_age |
+-----------+------------+-----------+------------+
| 1 | Michael | Phelps | |
| 2 | Katie | Ledecky | |
| 3 | Usain | Bolt | |
| 4 | Allyson | Felix | |
| 5 | Kevin | Durant | |
| 7 | Tom | Soyer | 25 |
+-----------+------------+-----------+------------+
6 rows in set (0.00 sec)
SELECT * FROM message;
+------------+-----------+-------------+--------------------------------------------+---------------------+
| message_id | sender_id | receiver_id | message | send_datetime |
+------------+-----------+-------------+--------------------------------------------+---------------------+
| 1 | 1 | 2 | Congrats on winning the 800m Freestyle! | 2016-12-25 09:00:00 |
| 2 | 2 | 1 | Congrats on winning 23 gold medals! | 2016-12-25 09:01:00 |
| 3 | 3 | 1 | You're the greatest swimmer ever | 2016-12-25 09:02:00 |
| 4 | 1 | 3 | Thanks! You're the greatest sprinter ever | 2016-12-25 09:04:00 |
| 5 | 1 | 4 | Good luck on your race | 2016-12-25 09:05:00 |
+------------+-----------+-------------+--------------------------------------------+---------------------+
5 rows in set (0.00 sec)
我的解决方案: 选择 sender_id、receiver_id、message_id、message、send_datetime FROM 消息传递.message 在哪里 sender_id = 1;
+-----------+-------------+------------+--------------------------------------------+---------------------+
| sender_id | receiver_id | message_id | message | send_datetime |
+-----------+-------------+------------+--------------------------------------------+---------------------+
| 1 | 2 | 1 | Congrats on winning the 800m Freestyle! | 2016-12-25 09:00:00 |
| 1 | 3 | 4 | Thanks! You're the greatest sprinter ever | 2016-12-25 09:04:00 |
| 1 | 4 | 5 | Good luck on your race | 2016-12-25 09:05:00 |
+-----------+-------------+------------+--------------------------------------------+---------------------+
我这样做对吗?如何显示发送者和接收者的名字和姓氏?还是我的做法正确?
【问题讨论】:
-
你必须加入表。搜索 SQL 内连接以获取信息