简体   繁体   English

我应该如何将这两个 SQL 查询合二为一?

[英]How Should I Combine These Two SQL queries into one?

I am trying to make an INNER JOIN statement that will join two tables, the Orders table and the Customer table, these both share the value/key of CustomerID.我正在尝试创建一个 INNER JOIN 语句,该语句将连接两个表,Orders 表和 Customer 表,它们都共享 CustomerID 的值/键。 The Customer table has the information for which state a customer lives in. The Orders table has the information for which customer, according to their customer ID, bought which product. Customer 表包含客户居住在哪个州的信息。 Orders 表包含根据客户 ID 购买哪种产品的客户的信息。 I need to find which products are the top 3 most popular in certain states.我需要找出哪些产品是某些州最受欢迎的前 3 种产品。 Please find the table descriptions images below, so you can understand what I mean.请找到下面的表格描述图片,以便您理解我的意思。

Orders table:订单表:

在此处输入图片说明

Customer table:客户表:

在此处输入图片说明

How can I make this INNER JOIN statement and include the logical operators (and/or) to make this happen?我怎样才能制作这个 INNER JOIN 语句并包含逻辑运算符(和/或)来实现这一点?

Thanks!谢谢!

Try this,尝试这个,

SELECT column_name(s)
FROM Customer
INNER JOIN Order
ON Customer.CustomerID= Order.Customer_ID AND <conditions>;

Inner Join is just only 1 way of joining 2 tables.内连接只是连接 2 个表的一种方式。 You can also join these two tables using WHERE closure as follows,您还可以使用WHERE闭包连接这两个表,如下所示,

SELECT column_name(s)
FROM Customer c, Order o
WHERE c.CustomerID = o.Customer_ID AND <condition>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM