sql server 语法 三个表格的连接
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql server 语法 三个表格的连接相关的知识,希望对你有一定的参考价值。
怎么用sql server 把三个表格连接起来
参考技术A SQL语言,数据库基础电子资料: SQLite权威指南 http://bbs.topsage.com/dispbbs.asp?boardID=105&ID=146072 SQL语法大全中文版 http://bbs.topsage.com/dispbbs.asp?boardID=105&ID=148128 SQL语言案头完全参考手册 http://bbs.topsage.com/dispbbs.asp?boardID=105&ID=146159 SQL - A Practical Introduction http://bbs.topsage.com/dispbbs.asp?boardID=105&ID=148095 O'Reilly SQL Tuning http://bbs.topsage.com/dispbbs.asp?boardID=122&ID=159156 O'Reilly The Art of SQL http://bbs.topsage.com/dispbbs.asp?boardID=122&ID=163611 数据库综合资料库 http://bbs.topsage.com/dispbbs.asp?boardID=105&ID=146238 数据库设计指南 http://bbs.topsage.com/dispbbs.asp?boardID=105&ID=148082 Wrox Beginning Database Design http://bbs.topsage.com/dispbbs.asp?boardID=105&ID=145206 SQL Puzzles and Answers http://bbs.topsage.com/dispbbs.asp?boardID=122&ID=160834 SQL Queries for Mere Mortals http://bbs.topsage.com/dispbbs.asp?boardID=122&ID=162978 SQL Puzzles and Answers http://bbs.topsage.com/dispbbs.asp?boardID=122&ID=160834 Apress出版 The Berkeley DB Book http://bbs.topsage.com/dispbbs.asp?boardID=122&ID=168290 数据库系统概论 浙江大学张军教授主讲(全32讲) http://bbs.topsage.com/dispbbs.asp?boardID=122&ID=154299 MS SQL Server电子资料: Transact-SQL Cookbook http://bbs.topsage.com/dispbbs.asp?boardID=122&ID=163150 SQL Server 2005宝典 http://bbs.topsage.com/dispbbs.asp?boardID=105&ID=149644 Microsoft SQL Server 2005完全参考 http://bbs.topsage.com/dispbbs.asp?boardID=105&ID=152872 O'Reilly Learning SQL on SQL Server 2005 http://bbs.topsage.com/dispbbs.asp?boardID=122&ID=163464 Beginning SQL Server 2005 Programming http://bbs.topsage.com/dispbbs.asp?boardID=105&ID=153216 Pro SQL Server 2005 High Availability http://bbs.topsage.com/dispbbs.asp?boardID=122&ID=162981 Beginning SQL Server 2005 Administration http://bbs.topsage.com/dispbbs.asp?boardID=105&ID=152863 SQL Server 2005 Unlea带有 TIMESTAMP 的 SQL Server 中的表连接问题
【中文标题】带有 TIMESTAMP 的 SQL Server 中的表连接问题【英文标题】:Table joining issue in SQL Server with TIMESTAMP 【发布时间】:2019-05-27 06:58:40 【问题描述】:我想从 SQL Server 中的三个表中获取当天的详细信息。我无法使用TIMESTAMP
加入表格,每个表格中的TIMESTAMP
都不同。连接这些表没有共同的因素。有什么办法可以加入这些表。而且我无法修改表格,这些表格来自第三方服务。
查询:
SELECT *
FROM table1
WHERE CAST(TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY CAST(TIMESTAMP AS DATETIME2(0))
SELECT *
FROM table2
WHERE CAST(TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY CAST(TIMESTAMP AS DATETIME2(0))
SELECT *
FROM table3
WHERE CAST(TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY CAST(TIMESTAMP AS DATETIME2(0))
SELECT
CAST(one.TIMESTAMP AS DATETIME2(0)) AS DATE,
CONVERT(DECIMAL(10, 2), one.value) AS v1,
CONVERT(DECIMAL(10, 2), two.VALUE) AS v2,
CONVERT(DECIMAL(10, 2), three.VALUE) AS v3
FROM
table1 one
LEFT JOIN
table2 two ON CAST(one.TIMESTAMP AS DATETIME2(0)) = CAST(two.TIMESTAMP AS DATETIME2(0))
LEFT JOIN
table3 three ON CAST(one.TIMESTAMP AS DATETIME2(0)) = CAST(three.TIMESTAMP AS DATETIME2(0))
WHERE
CAST(one.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
ORDER BY
CAST(one.TIMESTAMP AS DATETIME2(0))
结果:
我没有得到第 2 表和第 3 表的数据。例如:2018-12-30 00:00:15.657
这次是从第三张表的第一行开始。结果中没有显示。我想要所有数据。
希望你能理解。你能帮我解决这个问题吗?
【问题讨论】:
具体是什么问题?首先,它sez“无法加入”,然后“加入”完美运行。请说明预期结果,以及这与加入有何不同。然后,我们可以重现这一点,删除 jpeg 或 png 输入,并将它们作为副本替换为 headers/paste,然后用大括号框住每一个以保持间距。 @donPablo 我刚刚编辑了我的问题。 加入row_number? @P.Salmon which row_number?你是说身份证? 我假设您希望加入每个表上的第一个条目,每个表上的第二个条目等。您不能加入 id,因为它们不一样,但您可以根据每个表生成 row_number id 并加入。 docs.microsoft.com/en-us/sql/t-sql/functions/… 和 google sqlserver row_number 了解更多信息。 【参考方案1】:或者,只需修改 WHERE
WHERE
CAST(one.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
OR CAST(two.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
OR CAST(three.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
【讨论】:
这个步骤我试过了,但是不能正常工作。【参考方案2】:你可能想要full join
:
SELECT COALESCE(CAST(one.TIMESTAMP AS DATETIME2(0)),
CAST(two.TIMESTAMP AS DATETIME2(0)),
CAST(three.TIMESTAMP AS DATETIME2(0))
) as dt,
CONVERT(DECIMAL(10, 2), one.value) AS v1,
CONVERT(DECIMAL(10, 2), two.VALUE) AS v2,
CONVERT(DECIMAL(10, 2), three.VALUE) AS v3
FROM (SELECT *
FROM table1 one
WHERE CAST(one.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
) one FULL JOIN
(SELECT *
FROM table2 two
WHERE CAST(two.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
) two
ON CAST(two.TIMESTAMP AS DATETIME2(0)) = CAST(on.TIMESTAMP AS DATETIME2(0)) FULL JOIN
(SELECT *
FROM table3 three
WHERE CAST(three.TIMESTAMP AS DATE) = CAST(GETDATE() AS DATE)
) three
ON CAST(three.TIMESTAMP AS DATETIME2(0)) = COALESCE(CAST(one.TIMESTAMP AS DATETIME2(0)), CAST(one.TIMESTAMP AS DATETIME2(0)))
ORDER BY dt;
FULL JOIN
s 过滤起来很棘手,这就是它使用子查询的原因。
【讨论】:
以上是关于sql server 语法 三个表格的连接的主要内容,如果未能解决你的问题,请参考以下文章
4 - SQL Server 2008 之 使用SQL语句删除表格