关系数据库 SQL:Parent/Child 关系
Relational databases SQL: Parent/Child relationships
我正在努力学习SQL。我有三个不同的 table。所有 table 都有一个共同的列。我展示的是样本数据的一小部分。
我试图了解如何在 child table 更新后从 parent table 读取。
Table_1 (booking platform info)
booking_date column2 column3 column4 cust_country_id hotel_id booking_value
22-mar-2016 .................. 1001 1 0
01-apr-2016 .................. 1002 2 0
09-apr-2016 .................. 1001 2 2
17-apr-2016 .................. 1002 4
19-apr-2016 .................. 1003 1 0
03-May-2016 ................., 1001 3 1
Table_2 (hotel information)
hotel_id hotel_name hotel_country
1 Marriott Germany
2 Novotel France
3 Oberoi India
4 Osaka Japan
Table_3 (customer information)
country_id country_name
1001 India
1002 France
1003 Japan
我的问题是,如果在Table_2(酒店信息)中添加了一家来自法国的新凯悦酒店,我如何才能找到法国客户预订该酒店的第一天?
我对如何处理这个问题有点困惑;因为它必须从所有 3 个 table 中获取值。
我认为在这种情况下,您应该编写 mysql 查询以查找来自法国的客户和名为 Hyatt
的酒店的年龄最大的 booking_date
select booking_date from Table_1
join Table_2 on Table_2.hotel_id = Table_1.hotel_id
join Table_3 on Table_3.country_id = Table_1.cust_country_id
where Table_3.country_name = 'France' and Table_2.hotel_name = 'Hyatt'
order by Table_1.booking_date asc
limit 1
我正在努力学习SQL。我有三个不同的 table。所有 table 都有一个共同的列。我展示的是样本数据的一小部分。 我试图了解如何在 child table 更新后从 parent table 读取。
Table_1 (booking platform info)
booking_date column2 column3 column4 cust_country_id hotel_id booking_value
22-mar-2016 .................. 1001 1 0
01-apr-2016 .................. 1002 2 0
09-apr-2016 .................. 1001 2 2
17-apr-2016 .................. 1002 4
19-apr-2016 .................. 1003 1 0
03-May-2016 ................., 1001 3 1
Table_2 (hotel information)
hotel_id hotel_name hotel_country
1 Marriott Germany
2 Novotel France
3 Oberoi India
4 Osaka Japan
Table_3 (customer information)
country_id country_name
1001 India
1002 France
1003 Japan
我的问题是,如果在Table_2(酒店信息)中添加了一家来自法国的新凯悦酒店,我如何才能找到法国客户预订该酒店的第一天?
我对如何处理这个问题有点困惑;因为它必须从所有 3 个 table 中获取值。
我认为在这种情况下,您应该编写 mysql 查询以查找来自法国的客户和名为 Hyatt
的酒店的年龄最大的 booking_dateselect booking_date from Table_1
join Table_2 on Table_2.hotel_id = Table_1.hotel_id
join Table_3 on Table_3.country_id = Table_1.cust_country_id
where Table_3.country_name = 'France' and Table_2.hotel_name = 'Hyatt'
order by Table_1.booking_date asc
limit 1