两个查询和两个内部连接。

时间:2022-05-16 15:21:23

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

这里有两个查询。第一个显示了cat链接= 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?

但这似乎行不通,有什么解决办法吗?

3 个解决方案

#1


0  

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;

#2


2  

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.

您可以选择列表。*要显示表列表中的所有字段,或选择*显示所有3个连接表中的所有字段,或选择列表。*,账户。从这两个表中显示。

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'
;

#3


2  

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'

您没有指定“链接”在哪个表中,因此,如果您使用此代码(只要它执行了您想要的),我建议您指定“链接”字段在哪个表中:TableName。链接= ' 3 '

#1


0  

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;

#2


2  

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.

您可以选择列表。*要显示表列表中的所有字段,或选择*显示所有3个连接表中的所有字段,或选择列表。*,账户。从这两个表中显示。

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'
;

#3


2  

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'

您没有指定“链接”在哪个表中,因此,如果您使用此代码(只要它执行了您想要的),我建议您指定“链接”字段在哪个表中:TableName。链接= ' 3 '