SQL使用三个表的选择查询使用三个表

huangapple go评论113阅读模式
英文:

SQL Select Query Using Three Tables Using Three Tables

问题

我有3个表OrderPayment、OrderSummaryInfo和OrderLossEntity。

OrderPayment表有一个列OrdertypeCD,OrderLossEntity表有一个列OrderType。

这3个表之间没有直接关系。

OrderSummary表有一个主键orderevententity。这个表是两个表(OrderPayment和OrderLossEntity)的主表。

OrderSummaryInfo表有一个名为orderNumber的列,而OrderPayment表也有OrderNumber。这个OrderNumber是OrderPayment表的主键,引用了OrderSummaryInfo:

OrderSummaryInfo.OrderNumber = OrderPayment.OrderNumber
OrderSummaryInfo.orderevententity是OrderLossEntity.id的主键,OrderLossEntity表的主键是指向OrderSummaryInfo.orderevententity的:

OrderSummaryInfo.orderevententity = OrderLossEntity.id
我的需求是OrderTypeCD有3个可能的值“Cash”、“Gift”和“rewards”。

OrderType列有2个可能的值 - “Direct”和“Indirect”。

我需要从上述表中编写一个选择查询,其中OrderType为“Direct”且OrderTypeCD为“Cash”。

需要一个在SQL或PostgreSQL上的选择查询。

英文:

I have 3 Tables OrderPayment, OrderSummaryInfo and OrderLossEntity.

OrderPayment table has a column OrdertypeCD, OrderLossEntity table has a column OrderType.

There is no direct relation between all the 3 tables.

Ordersummary has a primary key orderevententity. This table is the master table for both the tables (OrderPayment and OrderLossEntity).

OrderSummaryinfo table has a column called orderNumber and OrderPayment table also has OrderNumber.
This OrderNumber is the primary key in OrderPayment table and is referring to OrderSummaryinfo:

OrderSummaryInfo.OrderNumber = OrderPayment.OrderNumber
OrderSummaryinfo.orderevententity is the primary key for OrderLosseventEntity.id --> where is the primary key in OrderLosseventEntity table referring to OrderSummaryinfo.orderevententity:

OrderSummaryinfo.orderevententity = OrderLossEntity.id
My requirement is OrderTypeCD has 3 possible values "Cash", "Gift" and "rewards".

OrderType column has 2 possible values - "Direct" and "Indirect".

I need a select query from the above tables where OrderType="Direct" and OrderTypeCD="Cash".

Need A Select Query on SQl Or PostgreSql

答案1

得分: 0

以下是查询的翻译部分(potsgresql):

SELECT * FROM OrderSummaryInfo /* 如果需要,从其他两个表中添加行 */
INNER JOIN OrderPayment ON OrderSummaryInfo.OrderNumber = OrderPayment.OrderNumber
INNER JOIN OrderLossEntity ON OrderSummaryInfo.orderevententity = OrderLossEntity.id
WHERE OrderLossEntity.OrderType = 'Direct' AND OrderPayment.OrderTypeCD = 'Cash'

有关如何编写连接查询的更多信息,请参阅 教程

英文:

Here is the query (potsgresql):

SELECT * FROM OrderSummaryInfo /* add rows from two other tables, if needed */
INNER JOIN OrderPayment ON OrderSummaryInfo.OrderNumber = OrderPayment.OrderNumber
INNER JOIN OrderLossEntity ON OrderSummaryInfo.orderevententity = OrderLossEntity.id
WHERE OrderLossEntity.OrderType = 'Direct' AND OrderPayment.OrderTypeCD = 'Cash'

For more info about how to write join queries, refer to the tutorial.

huangapple
  • 本文由 发表于 2023年6月19日 15:18:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定