SQL使用WHERE从多个表中获取数据

时间:2018-06-25 00:13:41

标签: sql mysqli

问题: 构造SQL语句以查找Michael Phelps发送的所有消息。注意:您必须使用WHERE 子句设置此查询的条件。显示以下列: -发件人的名字 -寄件人的姓氏 -收件人的名字 -收件人的姓氏 -讯息编号 - 信息 -邮件时间戳

表格:

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,消息,send_datetime 来自messages.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 |
+-----------+-------------+------------+--------------------------------------------+---------------------+

我这样做正确吗?如何显示发送者和接收者的名字和姓氏?还是我的操作方式正确?

2 个答案:

答案 0 :(得分:0)

您可以使用联接获取人名或您想要的任何名称。这是查询

select * from 
( select m.messageid,m.senderId,p.first_name as 'senderName' from message m inner join person p on ( p.person_id=m.senderId) 
) sen
inner join 
( select m.messageid,m.receiver_id,p.first_name as 'receiverName' from message m 
inner join person p on (  p.person_id=m.receiver_id)
) rec
on (rec.messageid=sen.messageid)
where sen.senderId=1

这是我创建两个临时表的说明(即,sen用于发送方,rec用于接收方),而不是使用messageId上的内部连接来连接两个表。希望结果表能为您提供所有想要的细节。

答案 1 :(得分:0)

SQL Fiddle

MySQL 5.6模式设置

create table contact_list(
    connection_id int,
    person_id int,
    contact_id int,
    favorite varchar(1)
);

insert into contact_list values (1, 1, 2, 'n');
insert into contact_list values (2, 1, 3, 'n');
insert into contact_list values (3, 1, 4, 'n');
insert into contact_list values (4, 1, 5, 'n');
insert into contact_list values (5, 1, 6, 'n');
insert into contact_list values (6, 2, 1, 'y');
insert into contact_list values (7, 2, 3, 'n');
insert into contact_list values (8, 2, 4, 'n');
insert into contact_list values (9, 3, 1, 'y');
insert into contact_list values (0, 3, 4, 'n');
insert into contact_list values (1, 4, 5, 'n');
insert into contact_list values (2, 4, 6, 'n');
insert into contact_list values (3, 5, 1, 'y');
insert into contact_list values (4, 5, 6, 'n');
insert into contact_list values (5, 7, 1, 'y');
insert into contact_list values (6, 7, 2, 'n');
insert into contact_list values (7, 7, 4, 'n');


create table person (
    person_id int,
    first_name varchar(200),
    last_name varchar(200),
    person_age int
);

insert into person values (1, 'Michael', 'Phelps', null);
insert into person values (2, 'Katie', 'Ledecky', null);
insert into person values (3, 'Usain', 'Bolt', null);
insert into person values (4, 'Allyson', 'Felix', null);
insert into person values (5, 'Kevin', 'Durant', null);
insert into person values (7, 'Tom', 'Soyer', 25);


create table message (
    message_id int,
    sender_id int,
    receiver_id int,
    message varchar(200),
    send_datetime DATETIME
);

insert into message values (1, 1, 2, 'Congrats on winning the 800m Freestyle!', '2016-12-25 09:00:00');
insert into message values (2, 2, 1, 'Congrats on winning 23 gold medals!', '2016-12-25 09:01:00');
insert into message values (3, 3, 1, 'You\'re the greatest swimmer ever', '2016-12-25 09:02:00');
insert into message values (4, 1, 3, 'Thanks!  You\'re the greatest sprinter ever', '2016-12-25 09:04:00');
insert into message values (5, 1, 4, 'Good luck on your race', '2016-12-25 09:05:00');

获取这些字段需要做什么

select 
  pSender.first_name as 'Sender\'s first name',
  pSender.last_name as 'Sender\'s last name',
  pReciever.first_name as 'Receiver\'s first name',
  pReciever.last_name as 'Receiver\'s last name',
  m.message_id as 'Message ID',
  m.message as 'Message',
  m.send_datetime as 'Message Timestamp'
from
  message m
  join person pSender on pSender.person_id = m.sender_id
  join person pReciever on pReciever.person_id = m.receiver_id

Results

| Sender's first name | Sender's last name | Receiver's first name | Receiver's last name | Message ID |                                    Message |    Message Timestamp |
|---------------------|--------------------|-----------------------|----------------------|------------|--------------------------------------------|----------------------|
|               Katie |            Ledecky |               Michael |               Phelps |          2 |        Congrats on winning 23 gold medals! | 2016-12-25T09:01:00Z |
|               Usain |               Bolt |               Michael |               Phelps |          3 |           You're the greatest swimmer ever | 2016-12-25T09:02:00Z |
|             Michael |             Phelps |                 Katie |              Ledecky |          1 |    Congrats on winning the 800m Freestyle! | 2016-12-25T09:00:00Z |
|             Michael |             Phelps |                 Usain |                 Bolt |          4 | Thanks!  You're the greatest sprinter ever | 2016-12-25T09:04:00Z |
|             Michael |             Phelps |               Allyson |                Felix |          5 |                     Good luck on your race | 2016-12-25T09:05:00Z |