Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL多表联接查询_Sql_Mysql_Join - Fatal编程技术网

MySQL多表联接查询

MySQL多表联接查询,sql,mysql,join,Sql,Mysql,Join,所需结果:格拉斯哥比尔斯登办事处教员使用的汽车注册号。 我有3个相关表格:;(为了简化,我省略了不相关的表信息。该数据库称为easydrive 不,这不是学校或大学的作业,这是课本上的问题,我们被要求这样做,这样我们就可以学习如何做,没有分数,所以你不是在为我做作业 **++Staff++** (PK) Staff ID (FK) Address ID (FK) Office (FK) Car Allocation Number First Name Last Name Position/Tit

所需结果:格拉斯哥比尔斯登办事处教员使用的汽车注册号。 我有3个相关表格:;(为了简化,我省略了不相关的表信息。该数据库称为easydrive

不,这不是学校或大学的作业,这是课本上的问题,我们被要求这样做,这样我们就可以学习如何做,没有分数,所以你不是在为我做作业

**++Staff++**
(PK) Staff ID
(FK) Address ID
(FK) Office
(FK) Car Allocation Number
First Name
Last Name
Position/Title
Office

**++CarAllocation++**
(PK) Car Allocation Number
(FK) Staff ID
(FK) Car ID

**++Car++**
(PK) Car ID
Car Rego
所以我需要加入,我想,我想它需要沿着这些路线走,但我很困惑

SELECT car.rego
FROM car
WHERE  staff.office=’Glasgow’ OR ‘Bearsden’

有人能帮我填空吗?这样我就可以学习如何执行此操作了,我需要创建一个新表吗?

至于
车辆分配
是一个中间表,最好将其放入
FROM
子句和
连接
其他:

SELECT car.rego
FROM car_allocation A
JOIN car ON A.car_id = car.car_id
JOIN stuff ON A.stuff_id = stuff.stuff_id
AND (staff.office = 'Glasgow' OR staff.office = 'Bearsden' -- OR some another condition, etc)
注意括号,它们是分组条件

如果有枚举,可以在运算符中使用


另外,
JOIN
的意思与您查询的
internaljoin

的意思相同,您最可能需要如下所示:

SELECT first_name, last_name, car_rego
FROM   staff
JOIN   carallocation ON (carallocation.staff_id = staff.staff_id)
JOIN   car ON (car.car_id = carallocation.car_id)
WHERE  staff.office = 'Glasgow' OR staff.office = 'Bearsden';

请注意,
JOIN
是MySQL中an的同义词。

我还没有测试过它,但这应该会有所帮助

select car.rego
from car
inner join carrallocation ca on ca.carid = car.carid
inner join staff s on ca.staffid = s.staffid
where s.office in ('Glasgow', 'Bearsden')

您缺少的是表之间的联接。您需要联接到中间表,以获得staff表和car表之间的关系。

当您在A.stuff\u id=stuff.stuff\u id上写入联接内容时,您的意思是在A.staff\u id=staff.staff\u id上联接工作人员?那么staff是代替stuff,还是stuff是一个临时表如果MySQL5.1数据库有帮助的话。哦,它工作了…所缺少的就是carallocation…car_分配,我早该看到的。
select car.rego
from car
inner join carrallocation ca on ca.carid = car.carid
inner join staff s on ca.staffid = s.staffid
where s.office in ('Glasgow', 'Bearsden')