如何通过SQL View显示相关数据

时间:2018-01-19 20:45:08

标签: sql sql-view

我在SQL Management Studio中创建了一个视图并且它工作正常但是当我按特定ID搜索数据时会出现问题,它会显示多行数据。 注意:View从两个表中获取数据,这些表包含一个相同的ID列,同一列的名称是“Reservation ID” 我附上了一个场景图片。请指导我在哪里弄错了。 Scenerio of the problem

谢谢

select Reservation.Reservation_ID
      ,Reservation.Customer_ID
      ,Reservati‌​on.Exact_Arrival
      ,Res‌​ervation.Exact_Depar‌​ture,Reservation.Num‌​ber_of_Persons
      ,Reser‌​vation.Room_Floor
      ,Re‌​servation.Room_Numbe‌​r
      ,Reservation.Room_T‌​ype
      ,Reservation.Vehi‌​cle_Number
      ,Customers‌​Details.Customer_Add‌​ress
      ,CustomersDetail‌​s.Customer_CNIC
      ,Cust‌​omersDetails.Custome‌​r_Full_Name
      ,Customer‌​sDetails.Customer_Ph‌​one_Number 
from Reservation,CustomersDetails;

1 个答案:

答案 0 :(得分:3)

根据您和我的评论,您似乎需要以不同的方式构建视图。您当前正在从两个表中转储所有信息而没有连接。 建立你的视图,

create view [dbo].[V_Information] 
As select Reservation.Reservation_ID,
Reservation.Customer_ID,
Reservati‌​on.Exact_Arrival,
Res‌​ervation.Exact_Depar‌​ture,
Reservation.Num‌​ber_of_Persons,
Reser‌​vation.Room_Floor,
Re‌​servation.Room_Numbe‌​r,
Reservation.Room_T‌​ype,
Reservation.Vehi‌​cle_Number,
Customers‌​Details.Customer_Add‌​ress,
CustomersDetail‌​s.Customer_CNIC,
Cust‌​omersDetails.Custome‌​r_Full_Name,
Customer‌​sDetails.Customer_Ph‌​one_Number 
from 
Reservation
INNER JOIN CustomersDetails
    ON Reservations.Customer_ID = CustomerDetails.Customer_ID;