加入 sql 服务器中的视图和表

Joining view and tables in sql server

我有以下表格。

客户 Customer_id, Customer_name

Customer_details Customer_id,Phone_no,Address

Order_details Order_id,Customer_id,Order_type

我创建了如下视图

  Create  view Orders_Analysis
  Select c.Customer_id,cd.phone_no,od.order_id
  From customer c
       inner join order_details od
          on c. Customer_id=od. Customer_id
       Inner join Where c. Customer_id=cd. Customer_id
          cd. Customer_id=c.Customer_id

现在使用上面的视图和前面提到的表,我必须只提取视图中属于特定 order_type 的那些记录。

大家可以推荐一个方法吗

首先,你的View有一些错误,修正错误后这是正确的View

Create view Orders_Analysis
  Select c.Customer_id,cd.phone_no,od.order_id
  From customer c
     INNER JOIN order_details od
        ON c.Customer_id = od.Customer_id
     INNER JOIN Customer_details CD 
        ON c.Customer_id = cd.Customer_id

现在您只想提取视图中属于特定 order_type 的那些记录。

解决方案一:- 因为您在视图中没有列 order_type 然后使用 INNER JOIN

SELECT * 
 FROM Orders_Analysis OA
      INNER JOIN Order_details OD
        ON OA.order_id = OD.order_id
 WHERE OD.Order_type = your value here

方案二:-

否则在视图中添加订单类型列

Create view Orders_Analysis
      Select c.Customer_id,cd.phone_no,od.order_id,od.order_type
      From customer c
         INNER JOIN order_details od
            ON c.Customer_id = od.Customer_id
         INNER JOIN Customer_details CD 
            ON c.Customer_id = cd.Customer_id

并使用

Select * from Orders_Analysis where Order_Type =  your value here

您需要将字段 od.order_type 添加到视图定义中的 Select 语句结果集:

SELECT c.Customer_id,cd.phone_no,od.order_id,od.Order_type

然后,如果您针对视图 运行 select,请在 order_type 字段上为您要查找的值指定一个 WHERE 子句。

更新您的观点和select还有订单类型

Select c.Customer_id,cd.phone_no,od.order_id, od.Order_type

您现在可以使用您创建的视图执行 select 查询

Select * from Orders_Analysis where Order_Type = "any value"