通过SQL连接三个表

时间:2019-01-04 14:12:58

标签: sql join

我有3个SQL表:

客户:

id
name
name2

书籍:

id
title
cID (contains the id of the customer)

销售:

id
value
bID (contains the id of the book)

如何连接3个表以得到这样的结果?

Customer name
Customer name 2
Book title
Sales value

2 个答案:

答案 0 :(得分:1)

因此:

select 
  c.name as "Customer name", 
  c.name2 as "Customer name 2", 
  b.title as "Book title", 
  s.value as "Sales value"
from 
  customers c 
  inner join books b on b.cID = c.id
  inner join sales s on s.bID = b.id

如果您的数据库不支持ANSI SQL概念“在列别名周围加上引号以允许它们具有空格”,请使用数据库通常使用的相关字符

答案 1 :(得分:0)

Select c.name, c.name2, b.title, s.value from customers c 
left join books b on c.id = b.cID
left join sales s on b.id = b.bID