CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Sql Command - 2 Inner Joins

    Not sure how to do this - keep getting a syntax error

    THIS WORKS (One Inner Join)

    Code:
    Select [ACCOUNT],  [KEY], [Stock].[DESC], [GRP], [CLASS], [PRICE], [FROM_DATE], [TO_DATE], [TYPE_CD] from [SpecialPrices] INNER JOIN  [Stock]  ON  [SpecialPrices].[KEY] = [Stock].[Key] ORDER BY [ACCOUNT] ASC
    I would like to do 2 Inner Joins so that I can have the Account Name alongside the Account Number

    So I presume it will look something like ...

    Code:
    Select [ACCOUNT],  [Debtor].[Account_Name],  [KEY], [Stock].[DESC], [GRP], [CLASS], [PRICE], [FROM_DATE], [TO_DATE], [TYPE_CD] from [SpecialPrices] INNER JOIN [Debtor] ON [SpecialPrices].[ACCOUNT] = [Debtor].[DR_KEY]   INNER JOIN  [Stock]  ON  [SpecialPrices].[KEY] = [Stock].[Key] ORDER BY [ACCOUNT] ASC
    The added code is BOLD but gives a syntax error

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Sql Command - 2 Inner Joins

    What is the syntax error? For example, MySQL returns something like, you have an error in your query NEAR 'FROM Assignments'.

    Could you have a duplicate field name in the list of fields your a pulling(IE, could there be a filed named [CLASS]/[PRICE]... in the Debtor table)?

  3. #3
    Join Date
    Aug 2006
    Location
    Hubli, India
    Posts
    70

    Re: Sql Command - 2 Inner Joins

    Try This ,

    Code:
    Select 
    `SpecialPrices`.`ACCOUNT`,  
    `Debtor`.`Account_Name`,  
    `SpecialPrices`.`KEY`, 
    `Stock`.`DESC`, 
    `GRP`, 
    `CLASS`, 
    `PRICE`, 
    `FROM_DATE`, 
    `TO_DATE`, 
    `TYPE_CD` 
    from `SpecialPrices` 
    INNER JOIN `Debtor` ON 
    `SpecialPrices`.`ACCOUNT` = `Debtor`.`DR_KEY`   
    INNER JOIN  `Stock`  ON  
    `SpecialPrices`.`KEY` = `Stock`.`Key` 
    ORDER BY `ACCOUNT` ASC

  4. #4
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    Re: Sql Command - 2 Inner Joins

    I'd have done it in following way:

    Code:
    SELECT Account, D.Account_Name,  SP.Key, St.Desc, Grp, Class, Price, From_Date, To_Date, Type_CD
    
    FROM SpecialPrices SP, Debtor D, Stock SP 
    WHERE SP.Account = D.Dr_Key
    AND SP.Key = St.Key
    ORDER BY Account ASC
    The only problem I found in your code : column 'Key' is ambigious. You haven't specified which 'Key' to read as there are two columns with same name in data source.

  5. #5
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: Sql Command - 2 Inner Joins

    Thanks so much Shaikh - you have achieved 2 Inner Joins without even specifying ONE !

    Your code also becomes much easier to follow - Great !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured