两个查询和两个内部联接在一起

发布于 2024-10-27 01:22:11 字数 594 浏览 2 评论 0原文

我这里有两个疑问。
第一个显示了 cat 链接 = 3 的列表。

Select * 
  from Listings 
  JOIN Category ON Listings.Category = Category.CategoryID 
 WHERE Link = '3'

第二个显示了连接到帐户的列表。

SELECT *  
  FROM Listings 
  JOIN Accounts ON Listings.Account_ID = Accounts.Account_ID

我的尝试是这样的:

SELECT * 
  FROM (Select * 
          from Listings 
          JOIN Category ON Listings.Category = Category.CategoryID 
         WHERE Link = '3') 
  JOIN Accounts ON Listings.Account_ID = Accounts.Account_ID

但这似乎不起作用,有什么解决方案吗?

I have two queries here.
First one shows listings where a cat link = 3.

Select * 
  from Listings 
  JOIN Category ON Listings.Category = Category.CategoryID 
 WHERE Link = '3'

And the second one shows listing connecting to accounts.

SELECT *  
  FROM Listings 
  JOIN Accounts ON Listings.Account_ID = Accounts.Account_ID

My try is something like:

SELECT * 
  FROM (Select * 
          from Listings 
          JOIN Category ON Listings.Category = Category.CategoryID 
         WHERE Link = '3') 
  JOIN Accounts ON Listings.Account_ID = Accounts.Account_ID

But that doesn't seem to work, any solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如歌彻婉言 2024-11-03 01:22:11

WHERE ... 应该位于两个连接之后。

您可以使用 SELECT Listings.* 显示 Listing 表中的所有字段,或者使用 SELECT * 显示所有 3 个联接表中的所有字段,或者使用 SELECT Listings.* , Accounts.* 从这两个表中显示,等等。

SELECT * 
FROM Listings l
  INNER JOIN Category c
    ON l.Category = c.CategoryID 
  INNER JOIN Accounts a
    ON l.Account_ID = a.Account_ID
WHERE c.Link = '3'
;

The WHERE ... should go after the two joins.

You can have SELECT Listings.* to show all fields from table Listing, or SELECT * to show all fields from all 3 joined tables, or SELECT Listings.*, Accounts.* to show from these 2 tables, etc.

SELECT * 
FROM Listings l
  INNER JOIN Category c
    ON l.Category = c.CategoryID 
  INNER JOIN Accounts a
    ON l.Account_ID = a.Account_ID
WHERE c.Link = '3'
;
凌乱心跳 2024-11-03 01:22:11

像这样的东西会起作用吗?

SELECT      Listings.*
FROM        Listings
INNER JOIN  Accounts ON Listings.Account_ID = Accounts.Account_ID
INNER JOIN  Category ON Category.CategoryID = Listings.Category
WHERE       Link = '3'

您没有指定“Link”位于哪个表中,因此如果您使用此代码(假设它执行您想要的操作),我建议您指定“Link”字段位于哪个表中,如下所示:WHERE TableName.Link = '3'

Would something like this work?

SELECT      Listings.*
FROM        Listings
INNER JOIN  Accounts ON Listings.Account_ID = Accounts.Account_ID
INNER JOIN  Category ON Category.CategoryID = Listings.Category
WHERE       Link = '3'

You didn't specify which table "Link" is in, so if you use this code (provided it does what you want), I'd recommend that you specify which table the "Link" field is in like so: WHERE TableName.Link = '3'

时光礼记 2024-11-03 01:22:11

你能发布错误吗?该错误很可能是告诉您所有表都必须有名称。这意味着您对临时表执行的子选择必须有一个表别名。

SELECT * FROM (Select * from Listings INNER JOIN Category ON Listings.Category = Category.CategoryID WHERE Link = '3') as T1 INNER JOIN Accounts ON T1.Account_ID=Accounts.Account_ID;

Can you post the error? Most likely the error is telling you that all tables must have names. Which means that your subselect that you're doing for a temp table MUST have a table alias.

SELECT * FROM (Select * from Listings INNER JOIN Category ON Listings.Category = Category.CategoryID WHERE Link = '3') as T1 INNER JOIN Accounts ON T1.Account_ID=Accounts.Account_ID;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文