我在编写一个非常简单的SQL查询时遇到问题,该查询选择所有三个表中的所有列并加入我创建的数据库中的三个表,并使用BETWEEN过滤使用出生日期的记录选择来过滤数据。
这些是我的表格:
- 此代码创建商店表 -
CREATE TABLE [SHATTKudlerFineFoods].dbo.tblStores
(
strStoreID varchar (4) CONSTRAINT PK_strStoreID PRIMARY KEY Not Null,
strStoreName varchar (20) Not Null,
strManagerName varchar (30) Not Null
)
- 此代码创建职务表 -
CREATE TABLE [SHATTKudlerFineFoods].dbo.tblJobTitle
(
jobJobID varchar (4) CONSTRAINT PK_jobJobID PRIMARY KEY Not Null,
jobJobClass varchar (60) Not Null,
jobTitle varchar (60) Not Null,
jobDescription varchar (300) Not Null,
jobStatus varchar (11) Not Null
)
- 此代码创建员工表 -
CREATE TABLE [SHATTKudlerFineFoods].dbo.tblEmployee
(
empEmpID varchar (3) CONSTRAINT PK_empEmployeeID PRIMARY KEY Not Null,
empLName varchar (20) Not Null,
empFName varchar (15) Not Null,
empAddress varchar (30) Not Null,
empCity varchar (20) Not Null,
empState varchar (2) Not Null,
empZIPCode varchar (5) Not Null,
empAreaCode varchar (3) Not Null,
empPhoneNo varchar (8) Not Null,
empGender varchar (1) Not Null,
empDOB smalldatetime Not Null,
empAge Tinyint Not Null,
empHireDate smalldatetime Not Null,
empNoOfExempt Tinyint Not Null,
empMaritalStatus varchar (3) Not Null,
empSalary money Not Null,
empStoreID varchar (4) CONSTRAINT FK_strStoreID FOREIGN KEY REFERENCES
tblStores(strStoreID) Not Null,
empJobID varchar (4) CONSTRAINT FK_jobJobID FOREIGN KEY REFERENCES
tblJobTitle(jobJobID) Not Null
)
这是我写的加入他们的查询:
USE [SHATTKudlerFineFoods]
SELECT *
FROM tblStores INNER JOIN tblEmployee
ON tblStores.strStoreID = tblEmployee.empStoreID
FULL OUTER JOIN tblStores
ON tblStores.strStoreID = tblJobTitle.ordStoreID
WHERE empDOB BETWEEN '1/1/1985' and '1/1/1999'
答案 0 :(得分:2)
SELECT *
FROM tblStores INNER JOIN tblEmployee
ON tblStores.strStoreID = tblEmployee.empStoreID
FULL OUTER JOIN tblJobTitle
ON tblEmployee.empJobId = tblJobTitle.jobJobId;
我认为第三个参考应该是tblJobTitle
而不是tblStores
,以及用于连接它们的正确列。
答案 1 :(得分:2)
SELECT *
FROM tblStores INNER JOIN tblEmployee
ON tblStores.strStoreID = tblEmployee.empStoreID
FULL OUTER JOIN tblJobTitle
ON tblEmployee.empJobID = tblJobTitle.jobJobID
WHERE empDOB BETWEEN '1/1/1985' and '1/1/1999'
这是我想象的可能有用的