mysql - 内连接mysql连接3个表

标签 mysql sql inner-join

我在使用 3 个表的内部联接时遇到问题..

我需要显示 cust_id、客户姓名、产品名称<-(来自产品表)和销售日期<--(来自销售表),我还需要按最近日期的顺序显示首先。

这就是我到目前为止所得到的

enter SELECT   
   customers.cust_id, 
   customers.forename, 
   customers.surname, 
   products.prod_name, 
   sales.Date_of_sale
FROM 
   customers
INNER JOIN 
   sales
ON 
   customers.cust_id = sales.cust_id; here

如果您能在这里帮助我,我真的很感激,谢谢..

最佳答案

只需向 products 表再添加一个 JOIN 并包含 ORDER BY 子句即可:

SELECT   
   c.cust_id, 
   c.forename, 
   c.surname, 
   p.prod_name, 
   s.Date_of_sale
FROM customers c
    INNER JOIN sales s ON c.cust_id = s.cust_id
    INNER JOIN products p ON s.product_id = p.product_id
ORDER BY s.Date_of_sale DESC

关于mysql - 内连接mysql连接3个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26414517/

相关文章:

MySQL - 是否可以使用列作为表名?

mysql - 为什么当我内部连接我的所有三个表时,我得到重复的数据值作为记录?

php - 如何在 Laravel 中编写这个 SQL 查询?

sql - 存储(和访问)历史记录的最佳方式是什么 1 :M relationships in a relational database?

mysql - 我应该使用一张 table 还是多张 table

mysql - SQL:为什么我们需要交易来订票?

mysql - 如何插入mysql View ?

mysql - 使用连接表中的两个计数编写 JOIN 查询

mysql - 两个连续行中日期之间的差异

php - 如何连接两个表并返回两个表中的所有行