views:

63

answers:

2

I am working to implement a data connection between my C# application and a SQL Server Express database. Please bear in mind I have not worked with SQL queries before.

I have the following relevant tables:

ArticlesCommon
ArticlesLocalized
CategoryCommon
CategoryLocalized

The ArticlesCommon table holds language independent information such as price, weight etc.

This is the statement for now:

SELECT * 
    FROM ArticlesCommon 
    INNER JOIN ArticlesLocalized ON ArticlesCommon.ID = ArticlesLocalized.ID 
WHERE ArticlesLocalized.Language = @language
ORDER BY ArticlesCommon.DateAdded

ArticlesCommon contains a category id for each row. Now, I want to use this to look up the localized information in CategoryLocalized and add it to the result, something like

SELECT *, CategoryLocalized.Name as CategoryName.

If I have gotten my point across, is this doable?

Edit:

Another question though. Both ArticlesLocalized and ArticlesCommon contain ID, which naturally are the same. I want to select everything from ArticlesCommon but everything except ID from ArticlesLocalized.. Is there a concise way to achieve this?

I guess that

SELECT * FROM .....

yield all columns from all joined tables?

A: 

Just add another Join

SELECT * FROM ArticlesCommon c 
   JOIN ArticlesLocalized L
       ON  a.ID = L.ID  
   JOIN CategoryLocalized CL
       ON  CL.ID = c.CategoryID  
WHERE ArticlesLocalized.Language = @language 
ORDER BY ArticlesCommon.DateAdded 

Use outer joins if not every row in ArticlesCommon has a CategoryId

SELECT * FROM ArticlesCommon c 
   JOIN ArticlesLocalized L
       ON  a.ID = L.ID  
   Left [OUTER] JOIN CategoryLocalized CL
       ON  CL.ID = c.CategoryID  
WHERE ArticlesLocalized.Language = @language 
ORDER BY ArticlesCommon.DateAdded 
  • [OUTER] is optional, as [Left] implies OUTER
Charles Bretana
Thank you. Wish I could set both of youse as correct answers, but I'll set the one who replied first, seeing as your replies were very similar.I've edited in another question, if you could help me with that I'd be grateful aswell.
Max Malmgren
+1  A: 

what is the key that you need to JOIN ON

Does this work?

SELECT * FROM ArticlesCommon 
INNER JOIN ArticlesLocalized ON
ArticlesCommon.ID = ArticlesLocalized.ID 
INNER JOIN CategoryLocalized ON ArticlesCommon.ID = CategoryLocalized.ID
WHERE ArticlesLocalized.Language = @language
ORDER BY ArticlesCommon.DateAdded
SQLMenace
If CategoryLocalized.ID is the same information on ArticlesLocalized.ID it should work. But if @Max is mixing concepts it won't. Perhaps ArticlesCommon has a CategoryLocalizedID field. In this case he would add this line INNER JOIN CategoryLocalized ON ArticlesCommon.CategoryLocalizedID = CategoryLocalized.ID
Junior Mayhé