简体   繁体   中英

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.

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'. 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.

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. 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:

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:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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