简体   繁体   English

使用SQL在MS Access中创建表联接

[英]Using SQL to create table joins in MS Access

I'm REALLY new to SQL and I'm really struggling with all but the simplest of joins - especially in MS Access. 我对SQL真的很陌生,除了最简单的联接外,我一直在挣扎,尤其是在MS Access中。

At this stage, all I want to do is create a query from two tables: 'tblUsers', with columns 'UserID' and 'User', and 'tblPayments' with columns 'PaymentID', 'User' and 'Authoriser'. 在此阶段,我要做的就是从两个表中创建一个查询:“ tblUsers”,其列为“ UserID”和“ User”,以及“ tblPayments”,其列为“ PaymentID”,“ User”和“ Authoriser”。 I want my query to contain all of this data and also to have columns 'UserID' and 'AuthoriserID', both of these ID numbers being taken from 'tblUsers', but clearly one will relate to the User and one to the authoriser. 我希望我的查询包含所有这些数据,并具有“ UserID”和“ AuthoriserID”列,这两个ID编号均取自“ tblUsers”,但很明显其中一个与用户有关,一个与授权者有关。

I'm sure this is far more simple than I'm making it but how should I do this? 我敢肯定这比我做的要简单得多,但是我应该怎么做呢?

Thanks in advance. 提前致谢。

Keep in mind that there are minor differences in syntax between SQL server SQL and Access SQL. 请记住,SQL Server SQL和Access SQL在语法上有细微的差别。 While in most cases a query written in Access will function normally on SQL server, the opposite is less true because of the shortcuts often used on SQL server (example: dropping the 'INNER' from the joins and the 'AS' when using an alias.) 尽管在大多数情况下,用Access编写的查询将在SQL Server上正常运行,但事实并非如此,因为在SQL Server上经常使用快捷方式(例如:在使用别名时从联接中删除“ INNER”,而在别名中删除“ AS” )

Access: 访问:

SELECT tblUsers.UserID, 
tblUsers.User, 
tblPayments.paymentID, 
tblUsers.User AS  Authoriser, 
tblUsers_1.UserID AS AuthoriserID

FROM (tblPayments 
INNER JOIN tblUsers AS tblUsers_1 
  ON tblPayments.AuthorisorID = tblUsers_1.UserID) 
INNER JOIN tblUsers 
  ON tblPayments.userID = tblUsers.UserID;

SQL Server: SQL Server:

SELECT tblUsers.UserID, 
tblUsers.User, 
tblPayments.paymentID, 
tblUsers.User Authoriser, 
tblUsers_1.UserID AuthoriserID

FROM tblPayments 
JOIN tblUsers tblUsers_1 
  ON tblPayments.AuthorisorID = tblUsers_1.UserID 
JOIN tblUsers 
  ON tblPayments.userID = tblUsers.UserID;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM