左外连接查询(我认为)
我有两个表,如下所示:
产品:id
category
name
description
active
Sales_sheets: id
product_id
link
product_id
是来自产品 id
的外键表
我写了一个准备好的语句 JOIN 像这样工作:
SELECT p.name, p.description, s.link FROM products AS p
INNER JOIN sales_sheets AS s ON p.id = s.product_id WHERE active=1 AND category=?
基本上是一个产品可以有 PDF 链接,但并非每个产品都会有销售表。因此,如果我尝试打开一个没有附加销售表的产品,那么它总是不返回任何行。
所以我想我必须使用 LEFT OUTER JOIN 代替 INNER JOIN,但这也不会返回任何行,我是否以错误的顺序命名表?我以前从未使用过 OUTER 连接?
I have two tables that look like this:
Products: id
category
name
description
active
Sales_sheets: id
product_id
link
product_id
is a foreign key from the products id
table
I wrote a prepared statement JOIN like this which works:
SELECT p.name, p.description, s.link FROM products AS p
INNER JOIN sales_sheets AS s ON p.id = s.product_id WHERE active=1 AND category=?
Basically a product can have a link to a PDF, but not every product will have a sales sheet. So if i try to bring up a product which doesn't have a sales sheet attached to it then it always returns no rows.
So i thought I'd have to use a LEFT OUTER JOIN in place of the INNER JOIN, but that returns no rows too, am I naming the tables in the wrong order? I've never had to use an OUTER join before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)