gpt4 book ai didi

mysql - 连接表的问题(6 个表)

转载 作者:行者123 更新时间:2023-11-29 19:01:31 25 4
gpt4 key购买 nike

我对 MySQL 很陌生,这也是我的第一个问题,如果有任何错误请原谅我。

我有 6 个表,我想使用这 6 个表来获取报告。我的一份报告用于访问所有 6 个表并根据我们编写的查询打印数据。现在的问题是我已经编写了查询,但这将返回大量重复数据。如何避免这种情况请指导我。

演示数据:

添加员工

 -------------------
| eid | name |
-------------------
| 1 | Mohanraj |
-------------------
| 2 | pradeep |
-------------------
| 3 | kumar |
-------------------
| 4 | Murali |
-------------------

添加车辆

 ---------------------
| vid | vnumber |
---------------------
| 1 | TN22BQ6226 |
---------------------
| 2 | TN37CM9014 |
---------------------
| 3 | TN38BR9217 |
---------------------
| 4 | TN38BT5680 |
---------------------

第三个表

                 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| tid | vid | emp_id | entry_date | pick_place | start_time | drop_place | stop_time | pickupkm | drops | type_of_trip | travelkm | tamt | dates |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 5-5-2017 | Airport | 03:15 am | ESI | 06:30 am | 10500 | 20500 | Cash | 10000 | 5000 |2017-05-05 |
| 2 | TN22BQ6226 | 3 | 6-5-2017 | Hopes | 09:44 am | ESI | 12:30 pm | 12500 | 2500 | Cash | 1250 | 3500 |2017-05-06 |
| 3 | TN22BQ6226 | 3 | 5-5-2017 | Place1 | 03:15 pm | Place2 | 06:30 pm | 1500 | 1800 | Cash | 300 | 1500 |2017-05-05 |

第四表

                    ---------------------------------------------------------------------------------------------------------------------------------------------------
| fid | vid | emp_id | expcal | exp1 | exp2 | exp3 | exp4 | exp5 | exp_amt | exp_desc | dates |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | | | 2017-05-05 |
| 2 | TN22BQ6226 | 3 | 50 | | | 50 | 50 | | | | 2017-05-06 |

五表

<小时/>
                     | fi_id | vid         | emp_id     | totkm     | totamt     | expenses     | today_balance     | handover_amt     | balance_amt    | handover_to     | plstatus     | entry_date     |     entry_time |               
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 10000 | 5000 | 100 | 4900 | 4000 | 400 | ram | PROFIT | 2017-05-05 | 04:35:21 |
| 2 | TN22BQ6226 | 3 | 1250 | 3500 | 100 | 3400 | 3000 | 200 | raj | PROFIT | 2017-05-06 | 04:36:58 |

转变

                         ---------------------------------------------------------------------------------------------------------------------------------------------------
| sid | emp_id | vid | opeing_km | opeing_cash | closing_km | closing_cash | opeing_date | opeing_time | closing_date | closing_time | status |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | 1 |
| 2 | 3 | TN22BQ6226 | 20500 | 5000 | 1250 | 0 | 2017-05-06 | 04:34:55 | 2017-05-06 | 04:36:58 | 1 |

我尝试了这个查询,结果出错了,

SELECT a.vnumber
, a.vname
, b.eid
, b.name
, b.mobile
, c.tid
, c.vid
, c.emp_id
, c.pick_place
, c.start_time
, c.drop_place
, c.stop_time
, c.pickupkm
, c.drops
, c.type_of_trip
, c.travelkm
, c.tamt
, c.dates
, d.vid
, d.emp_id
, d.expcal
, d.exp1
, d.exp2
, d.exp3
, d.exp4
, d.exp5
, d.expamt
, d.expdesc
, d.dates
, e.emp_id
, e.vid
, e.opeing_km
, e.opeing_cash
, e.closing_km
, e.closing_cash
, e.opeing_date
, e.opeing_time
, e.closing_date
, e.closing_time
, f.vid
, f.emp_id
, f.totkm
, f.totamt
, f.expenses
, f.handover_amt
, f.balance_amt
, f.handover_to
, f.plstatus
, f.entry_date
FROM add_vehicle a
JOIN third_table c
ON a.vnumber = c.vid
JOIN add_employees b
ON b.eid = c.emp_id
JOIN fourth_table d
ON a.vnumber = d.vid
JOIN shift e
ON b.eid = e.emp_id
JOIN five_table f
ON a.vnumber = f.vid
, (SELECT @rownum := 0) r
WHERE c.type_of_trip IS NOT NULL
AND c.dates BETWEEN '2017-05-05' AND '2017-06-05'
AND d.dates BETWEEN '2017-05-05' AND '2017-06-05'
AND f.entry_date BETWEEN '2017-05-05' AND '2017-06-05'
AND c.vid = 'TN22BQ6226'
AND f.vid = 'TN22BQ6226'
AND e.vid = 'TN22BQ6226'
GROUP
BY c.tid
ORDER
BY c.dates
, f.entry_date DESC

我得到 Fourth_table 和 Five_table 值像这样重复。 onetwo

我想要这样的输出,

                         |  vnumber   |    vname          | eid          | name     |  tid     | vid          | emp_id     | pick_place   | start_time     | drop_place    | stop_time     |  pickup_km  | drops  | type_of_trip | travel_km | tamt |  dates           | vid          | emp_id |expcal |exp1 | exp2    | exp3 | exp4 | exp5 | expamt| expdesc | dates     | emp_id | vid         | opening_km | opening_cash |  closing_km | closing_cash | opeing_date | opeing_time | closing_date | closing_time | vid           | emp_id | totkm | totamt |     expenses |  handover_amt | balance_amt|  handover_to | plstatus | entry_date |     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 1 | TN22BQ6226 | 3 | Airport | 03:15 am | ESI | 06:30 am | 10500 | 20500 | Cash | 10000 | 5000| 2017-05-05 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | 100 | |2017-05-05 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | TN22BQ6226 | 3 | 10000| 5000 | 100 | 4000 | 400 | ram | PROFIT | 2017-05-05 |
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 2 | TN22BQ6226 | 3 | Hopes | 09:44 am | ESI | 12:01 pm | 1250 | 2500 | Cash | 1250 | 3500| 2017-05-06 | TN22BQ6226 | 3 | 50 | | | 50 | 50 | | 50 | |2017-05-06 | 3 | TN22BQ6226 | 20500 | 5000 | 1250 | 0 | 2017-05-06 | 04:34:55 | 2017-05-06 | 04:36:58 | TN22BQ6226 | 3 | 1250| 3500 | 100 | 3400 | 200 | raj | PROFIT | 2017-05-06 |
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 3 | TN22BQ6226 | 3 | place one | 03:15 pm | place two | 06:15 pm | 1500 | 1800 | Cash | 300 | 1500| 2017-05-05 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | 100 | |2017-05-05 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | TN22BQ6226 | 3 | 10000| 5000 | 100 | 4000 | 400 | ram | PROFIT | 2017-05-05 |

很抱歉问了一个大问题。

最佳答案

评论太长了...

让我们首先将问题简化如下:

SELECT a.vid
, d.emp_id
, d.expcal
, f.entry_date
, f.entry_time
FROM add_vehicle a
JOIN fourth_table d
ON d.vid = a.vnumber
JOIN five_table f
ON f.vid = a.vnumber

vid emp_id expcal entry_date entry_time
1 3 100 2017-05-05 04:35:21
1 3 50 2017-05-05 04:35:21
1 3 100 2017-05-06 04:36:58
1 3 50 2017-05-06 04:36:58

http://sqlfiddle.com/#!9/62f3e5/21

仅考虑上面提供的列,这与期望的结果有何不同?

顺便说一句 - 始终将日期和时间存储为单个实体

关于mysql - 连接表的问题(6 个表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43841648/

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