Reputation: 1
I am working with MSSQL. As long as I have been working on a project with a customer who bought our software, we are at the final step. At that point we have to give them some SQL queries to get some reports from MSSQL database.
As I am not a familiar with SQL I am having a problem with joining multiple tables.
I have searched some and created a query but the problem is joining a table with others which has no common column with the table I noticed after FROM tag.
Let me explain a little:
I have 5 tables "x", "y", "z", "w", "m".
"x" table have common columns with "y", "z" and "w" tables
"m" table has a common column with "y" table
I want to select one another column from "m" for my report how can I join "x" with that table,
How can I do this?
Upvotes: 0
Views: 2342
Reputation: 1735
Since M, Y and X have something in common, you can use the code below
SELECT
M.Col,
X,CoL
FROM M
INNER JOIN Y ON M.ID = Y.ID
INNER JOIN X ON Y.ID=X.ID
Upvotes: 0
Reputation: 1842
This is, indeed, a very basic question, but here you go.
SELECT *
FROM X
INNER JOIN Y ON X.XYColumn = Y.XYColumn
INNER JOIN M ON M.YMColumn = Y.YMColumn
Upvotes: 1
Reputation: 26
You can join x to y, and then join the combination of x and y to m. You don't need to select any of the columns from y if you want your result to have only columns from x and m. Something like:
SELECT x.column_from_x, m.column_from_m
FROM x
JOIN y
ON x.xy_common_column = y.xy_common_column
JOIN m
ON m.my_common_column = y.my_common_column
Any WHERE
clause you might want can follow this. There's a more concrete example here: https://technet.microsoft.com/en-us/library/ms191430(v=sql.105).aspx
Upvotes: 1