Using SQL to create table joins in MS Access

情到浓时终转凉″ 提交于 2019-12-25 04:38:08

问题


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.


回答1:


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;


来源:https://stackoverflow.com/questions/30358561/using-sql-to-create-table-joins-in-ms-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!