开发者

two queries and two inner joins together

开发者 https://www.devze.com 2023-02-20 08:31 出处:网络
I have two queries here. First one shows listings where a cat link = 3. Select * from Listings JOIN Category ON Listings.Category = Category.CategoryID

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?


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


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'


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

精彩评论

暂无评论...
验证码 换一张
取 消