内部连接,SQL混淆

时间:2018-12-18 22:52:00

标签: mysql

这是我班的挑战。 使用INNER JOIN检索以下信息:

  1. 使用“客户和订单”表查找由公司所有者创建的所有订单。

我想到了这个,但是当我输入它时,它说是空集>>>

public class TeamForCreationDto
    {
        int Id { get; set; }
        public string Name { get; set; }
        public string PhotoUrl { get; set; }
        public int[] MembersId { get; set; }
    }

这些是“客户”表中的列。 客户

SELECT id FROM orders INNER JOIN customers USING (id);

这些是订单表中的列。 订单

 | id   | company | last_name | first_name  | email_address | job_title       
 | business_phone  | home_phone | mobile_phone | fax_number | address 
 | city | state_province  |  zip_postal_code |  country_region  | web_page    
 | notes | attachments |.

2 个答案:

答案 0 :(得分:1)

写时:

SELECT id FROM orders INNER JOIN customers USING (id);

您正在请求:

SELECT o.ID              -- or c.ID
  FROM Orders AS o
  JOIN Customers AS c
    ON O.ID = C.ID;

您要问“哪些客户的ID与订单ID相同?”,这可能不是您的意思。

最有可能,您希望使用以下变量:

SELECT o.ID AS Order_ID, c.ID AS Customer_ID
  FROM Orders AS o
  JOIN Customers AS c
    ON O.Customer_ID = C.ID;

您可以选择在选择列表中获得的信息。

答案 1 :(得分:0)

您可以找到内部联接语法here的示例。这是如何执行此操作的一个示例:

select o.* 
from orders o
inner join customers c on o.customer_id = c.id
/* not sure how "owner of company" is represented in the data, but I'm assuming it's some value for job_title */
where c.job_title = 'owner'