如何 select 来自 mysql 数据库中不同表的特定列 php

How to select specific columns from different tables in mysql database in php

我有 3 个 table:

request(RequestID,Name,Mobie Number,Vehicle Number,Location)
reqresponse(RequestID,ResponseID,MechanicID,MechanicName)
mechdetails(MechanicID,MechanicName,MechanicMobile,MechanicAvalability)

现在,我想要做的是 select 来自 request table 的所有数据,并将其与 mechdetails table 的详细信息一起显示,其中 MechanicAvailability 设置为 'busy'。

RequestIDreqresponse中的外键table.

谁能告诉我 MySQL 代码。

我在PHP工作。

这是我现在所在的位置:

SELECT * FROM mechdetails WHERE MechAvailability='Busy' AND 
MechanicID=(SELECT MechanicID FROM reqresponse WHERE ResponseID='$rid')

我是 MySQL 和 PHP 的新手所以请帮助我!

您可以join表格:

select r.*, m.*
from request r
inner join reqresponse rr on rr.requestid = r.requestid
inner join mechdetails md on md.mechanicid = rr.mechanicid
where md.mechanicavailability = 'busy'

查询

SELECT * FROM request r
  JOIN reqresponse q ON q.RequestID = r.RequestID
  JOIN mechdetails m ON m.MechanicID = q.MechanicID
  WHERE MechanicAvailability = 'busy'

应该完成这项工作。

要访问生成的数据,请使用 mysqli_query and mysqli_result