SQL 查询连接表数据
SQL query join tables data
我有 2 tables Users
和 TimeSheets
,Timesheets
table 有列 UserId
这是用户 ID 来自用户 table 显然 :)
逻辑
我正在尝试获取 Timesheets
table 的所有数据加上 来自用户 table 的用户名,但不知何故好像我的查询是相反的!
现在我知道这可能是一个简单的查询,但我个人不知道 SQL(我的错!)
这是我的查询:
CREATE PROCEDURE [dbo].[GetTimeSheets]
AS
BEGIN
SELECT
t.Id, t.Enter_time, t.Exit_Time, t.Total, t.Date,
t.Month, t.Year, u.Name AS username
FROM
TimeSheets t
FULL OUTER JOIN
Users u ON u.Id = t.UserId
GROUP BY
t.Id, t.Enter_time, t.Exit_Time, t.Total, t.Date,
t.Month, t.Year, u.Name
END;
这里是返回数据的截图:
如您所见,我得到了我的用户名,但时间表中没有任何数据 table!
此视图应该获取时间表 table,然后是用户每一行的用户名。
更新
这是我的时间表table 架构
CREATE TABLE [dbo].[TimeSheets]
(
[Id] INT NOT NULL PRIMARY KEY,
[UserId] INT NOT NULL,
[Enter_time] TIME NOT NULL,
[Exit_Time] TIME NULL,
[Total] TIME NULL,
[Date] DATE NULL,
[Month] NVARCHAR(50) NOT NULL ,
[Year] NVARCHAR(50) NOT NULL ,
CONSTRAINT [FK_TimeSheets_Users]
FOREIGN KEY ([UserId]) REFERENCES [dbo].[Users]([Id])
);
这里是用户 table
CREATE TABLE [dbo].[Users]
(
[Id] INT NOT NULL,
[UserType] NVARCHAR (50) NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
您似乎想要一个 INNER
连接,并且由于您没有进行任何聚合,因此不需要 GROUP BY
:
SELECT t.*,
u.Name AS username
FROM TimeSheets t
INNER JOIN Users u
ON u.Id = t.UserId;
通过执行 INNER
联接,只有当 2 个表中的 2 行之间存在匹配时,您才会得到结果。
好吧,为了访问用户,您必须将用户 table 提供给 TimeSheets LEFT JOIN,这样如果用户配置文件为空。
SELECT t.Id, t.Enter_time, t.Exit_Time, t.Total, t.Date, t.Month, t.Year, u.Name as username
FROM Users u
LEFT JOIN TimeSheets t ON u.Id = t.UserId
GROUP BY t.Id,t.Enter_time,t.Exit_Time,t.Total, t.Date, t.Month, t.Year, u.Name
我有 2 tables Users
和 TimeSheets
,Timesheets
table 有列 UserId
这是用户 ID 来自用户 table 显然 :)
逻辑
我正在尝试获取 Timesheets
table 的所有数据加上 来自用户 table 的用户名,但不知何故好像我的查询是相反的!
现在我知道这可能是一个简单的查询,但我个人不知道 SQL(我的错!)
这是我的查询:
CREATE PROCEDURE [dbo].[GetTimeSheets]
AS
BEGIN
SELECT
t.Id, t.Enter_time, t.Exit_Time, t.Total, t.Date,
t.Month, t.Year, u.Name AS username
FROM
TimeSheets t
FULL OUTER JOIN
Users u ON u.Id = t.UserId
GROUP BY
t.Id, t.Enter_time, t.Exit_Time, t.Total, t.Date,
t.Month, t.Year, u.Name
END;
这里是返回数据的截图:
如您所见,我得到了我的用户名,但时间表中没有任何数据 table!
此视图应该获取时间表 table,然后是用户每一行的用户名。
更新
这是我的时间表table 架构
CREATE TABLE [dbo].[TimeSheets]
(
[Id] INT NOT NULL PRIMARY KEY,
[UserId] INT NOT NULL,
[Enter_time] TIME NOT NULL,
[Exit_Time] TIME NULL,
[Total] TIME NULL,
[Date] DATE NULL,
[Month] NVARCHAR(50) NOT NULL ,
[Year] NVARCHAR(50) NOT NULL ,
CONSTRAINT [FK_TimeSheets_Users]
FOREIGN KEY ([UserId]) REFERENCES [dbo].[Users]([Id])
);
这里是用户 table
CREATE TABLE [dbo].[Users]
(
[Id] INT NOT NULL,
[UserType] NVARCHAR (50) NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
您似乎想要一个 INNER
连接,并且由于您没有进行任何聚合,因此不需要 GROUP BY
:
SELECT t.*,
u.Name AS username
FROM TimeSheets t
INNER JOIN Users u
ON u.Id = t.UserId;
通过执行 INNER
联接,只有当 2 个表中的 2 行之间存在匹配时,您才会得到结果。
好吧,为了访问用户,您必须将用户 table 提供给 TimeSheets LEFT JOIN,这样如果用户配置文件为空。
SELECT t.Id, t.Enter_time, t.Exit_Time, t.Total, t.Date, t.Month, t.Year, u.Name as username
FROM Users u
LEFT JOIN TimeSheets t ON u.Id = t.UserId
GROUP BY t.Id,t.Enter_time,t.Exit_Time,t.Total, t.Date, t.Month, t.Year, u.Name