gpt4 book ai didi

php - 如何连接四个表来显示具体数据

转载 作者:行者123 更新时间:2023-11-29 10:01:15 26 4
gpt4 key购买 nike

我在连接表时遇到一些问题。我有四张 table

‘装备’表

equipment_id   equipment_name
----------- --------------
1 Motor Cycle
2 Private Car

====================================

这是我的项目表

<小时/>
project_id    project_name
---------- ------------
1 xyz trading
2 abc private ltd

==================================

这是租赁表

<小时/>
rental_id      project_id    equipment_id   Rental_date
---------------------------------------------------------
1 2 1 12/10/2018
2 2 2 15/09/2018
3 1 2 20/10/2018

这是删除表

<小时/>
remove_id      project_id    equipment_id   removal_date
---------------------------------------------------------
1 2 1 null
2 2 2 14/10/18

these are my table, now i want to display data of single equipment (eg: equipment no 2) in which project this equipment ran, when was the rental date and when was the removal date from a project.

我想像这样显示..(使用Codeigniter)

设备日志2

=======

    SN   Project Name      Rental date       Removal date    ---  ------------      -----------       ------------    1    ABC pvt Ltd        15/09/2018       14/10/2018    2    xyz trading        20/10/2018         Null

Controller passes to Model

$data['eqipment_details']=$this->Equipment_model->getLog($equipment_id);

请帮我查询

最佳答案

必须猜测您的表名称是什么,但您应该能够据此进行调整:

SELECT
(@cnt := @cnt + 1) AS SN,
PROJECT.project_name AS Project Name,
RENTAL.Rental_date AS Rental date,
REMOVAL.removal_date AS Removal date
FROM
rental_table RENTAL
LEFT JOIN project_table PROJECT ON (RENTAL.project_id = PROJECT.id)
LEFT JOIN equipment_table EQUIPMENT ON (RENTAL.equipment_id = EQUIPMENT.id)
LEFT JOIN removal_table REMOVAL ON (PROJECT.id = REMOVAL.project_id AND PROJECT.equipment_id = REMOVAL.equipment_id)
WHERE
EQUIPMENT.id = 2;

我强烈建议您与混合使用驼峰式大小写和标题大小写以及下划线的命名约定保持一致。

关于php - 如何连接四个表来显示具体数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52907492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com