如何从多表中选择输出?

时间:2019-07-20 08:13:53

标签: mysql

公共汽车

+----------+-------------+-------------+
| route_no | Start_point | destination |
+----------+-------------+-------------+
|      123 | ABC         | BCD         |
|      234 | DEF         | EFG         |
|      345 | GHI         | HIJ         |
|      445 | JKL         | KLM         |
|      546 | MNO         | NOP         |
+----------+-------------+-------------+

乘客

+-----+--------+--------+------+
| pid | pname  | gender | age  |
+-----+--------+--------+------+
| 111 | David  | m      |   30 |
| 222 | Andy   | f      |   20 |
| 333 | kat    | f      |   27 |
| 444 | viki   | m      |   32 |
| 555 | rob    | m      |   52 |
+-----+--------+--------+------+

预订

+-----+----------+------------+---------+
| pid | route_no | jrny_date  | seat_no |
+-----+----------+------------+---------+
| 111 |      123 | 2019-05-14 |      57 |
| 222 |      234 | 2019-06-11 |       3 |
| 333 |      345 | 2019-07-20 |      33 |
| 444 |      445 | 2018-08-22 |      14 |
| 555 |      546 | 2018-11-17 |      19 |
+-----+----------+------------+---------+`

1)我正在尝试显示从ABC到BCD的route_no = 123上的所有乘客。

2)显示当前日期在route_no = 345上旅行的所有乘客的所有pid和性别。

select passenger.*
  from passeger
     , bus
     , booking 
 where passenger.pid = booking.pid;

错误1146(42S02):表'travel_agency.passenger'不存在。

1 个答案:

答案 0 :(得分:-1)

mysql>从 passeger ,bus,booking中选择passenger。*,其中passenger.pid = booking。 pid;

是乘客吗?可能是这个错误。

要在您的答案查询中此处

1)

select passenger.pname, passenger.gender, passenger.age, bus.Start_point, bus.destination from passenger,bus,booking where passenger.pid=booking.pid and booking.route_no= bus.route_no and bus.route_no=123;

2)

select passenger.pname, passenger.gender, booking.jrny_date from passenger,booking where passenger.pid=booking.pid and booking.route_no=345;