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.
我正在使用MSSQL。只要我与购买我们软件的客户一起开展项目,我们就是最后一步。那时我们必须给他们一些SQL查询来从MSSQL数据库中获取一些报告。
As I am not a familiar with SQL I am having a problem with joining multiple tables.
由于我不熟悉SQL,因此加入多个表时遇到问题。
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.
我搜索了一些并创建了一个查询,但问题是加入一个表与其他没有共同列的表与我在FROM标记后注意到的表。
Let me explain a little:
让我解释一下:
I have 5 tables "x", "y", "z", "w", "m".
我有5个表“x”,“y”,“z”,“w”,“m”。
"x" table have common columns with "y", "z" and "w" tables
“x”表具有带“y”,“z”和“w”表的公共列
"m" table has a common column with "y" table
“m”表有一个带有“y”表的公共列
I want to select one another column from "m" for my report how can I join "x" with that table,
我想从“m”中为我的报告选择另一列,如何将“x”与该表连接,
How can I do this?
我怎样才能做到这一点?
3 个解决方案
#1
1
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:
您可以将x连接到y,然后将x和y的组合连接到m。如果希望结果只包含x和m的列,则无需从y中选择任何列。就像是:
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
您可能想要的任何WHERE子句都可以遵循此。这里有一个更具体的例子:https://technet.microsoft.com/en-us/library/ms191430(v = sql.105).aspx
#2
1
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
#3
0
Since M, Y and X have something in common, you can use the code below
由于M,Y和X有一些共同点,您可以使用下面的代码
SELECT
M.Col,
X,CoL
FROM M
INNER JOIN Y ON M.ID = Y.ID
INNER JOIN X ON Y.ID=X.ID
#1
1
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:
您可以将x连接到y,然后将x和y的组合连接到m。如果希望结果只包含x和m的列,则无需从y中选择任何列。就像是:
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
您可能想要的任何WHERE子句都可以遵循此。这里有一个更具体的例子:https://technet.microsoft.com/en-us/library/ms191430(v = sql.105).aspx
#2
1
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
#3
0
Since M, Y and X have something in common, you can use the code below
由于M,Y和X有一些共同点,您可以使用下面的代码
SELECT
M.Col,
X,CoL
FROM M
INNER JOIN Y ON M.ID = Y.ID
INNER JOIN X ON Y.ID=X.ID