簡體   English   中英

在 mysql php 的一行中顯示多個表值

[英]display multiple table value in one row in mysql php

我有一個 2 表表 A 和表 B 我想將表合並到一個表中..我想要表的 qurrey ..我試過這個 querry ..

SELECT COUNT(tablea.col_date)as totaljobs ,trans.collect_date, 
 ROUND(SUM(tablea.cloth_weight),2)as cloth_weight, 
 ROUND(SUM(tablea.cloth_amount),2) as clothes_amount, 
  ROUND(SUM(tablea.shoes_weight),2)as cloth_weight, 
 ROUND(SUM(tablea.shoes_amount),2) as clothes_amount,
 tablea.col_date,tablea.driver,tableb.fuel,tableb.expense FROM tablea INNER JOIN tableb ON tablea.driver=tableb.driver WHERE tablea.col_date BETWEEN '2022-09-20' AND date(now()) GROUP BY col_date,tableb.driver;

表 A:

ID col_date 衣服_kg 衣服數量 鞋子_kg 鞋量 司機
1 2022-09-20 50 25 30 15 約翰
2 2022-09-20 50 25 30 15 約翰
3 2022-09-20 30 15 40 20 約翰
4 2022-09-20 40 20 40 20
5 2022-09-20 40 20 20 10
6 2022-09-20 50 25 20 10
7 2022-09-21 50 25 20 10 約翰
8 2022-09-21 30 15 40 20 約翰
9 2022-09-21 60 30 40 20 約翰
10 2022-09-21 40 20 40 20
11 2022-09-21 30 15 40 20
12 2022-09-21 20 10 50 25
13 2022-09-22 50 25 50 25
14 2022-09-22 60 30 50 25
15 2022-09-22 70 35 50 25 約翰
16 2022-09-22 80 40 30 15 約翰

表 B:

ID 司機 col_date 汽油 Van_number
1 約翰 2022-09-20 30 3312
2 2022-09-20 30 3314
3 約翰 2022-09-21 0 3312
4 2022-09-21 0 3314
5 約翰 2022-09-22 0 3312
6 2022-09-22 0 3314

Output:

司機 col_date 汽油 Van_number 總和(衣服重量) 總和(衣服數量) 總和(鞋重) 總和(鞋量)
1 約翰 2022-09-20 30 3312 130 65 100 50
2 2022-09-20 30 3314 130 65 80 40
3 約翰 2022-09-21 0 3312 130 70 60 30
4 2022-09-21 0 3314 90 45 90 45
5 約翰 2022-09-22 0 3312 110 55 100 50
6 2022-09-22 0 3314 150 75 80 40

您嘗試的代碼走在正確的軌道上,添加它做得很好。 您需要在GROUP BY中包含所有非聚合列,並確保在JOIN中使用所有鍵列。 在這種情況下,您只加入了driver ,但您需要加入driver AND col_date 您的示例代碼中有一個trans.collect_date ,不確定它來自哪里。

我認為這會幫助你:

小提琴手

SELECT 
 tablea.col_date,
 tablea.driver,
 tableb.fuel,
 tableb.Van_number,
 ROUND(SUM(tablea.clothe_kg),2) as cloth_weight, 
 ROUND(SUM(tablea.clothe_amount),2) as clothes_amount, 
 ROUND(SUM(tablea.shoes_kg),2) as cloth_weight, 
 ROUND(SUM(tablea.shoes_amount),2) as clothes_amount 
FROM tablea 
INNER JOIN tableb 
  ON tablea.driver = tableb.driver 
  AND tablea.col_date = tableb.col_date
WHERE tablea.col_date BETWEEN '2022-09-20' AND date(now()) 
GROUP BY 
 tablea.col_date,
 tablea.driver,
 tableb.fuel, 
 tableb.Van_number;

從數據庫中的兩個表中獲取數據並顯示在單個前端表中非常容易,我希望您能夠連接到數據庫。

從表中獲取數據

$query1  = 'select * from A';
$query2 = 'select * from B';

$result1  = mysqli_query($con , $query1);
$result2  = mysqli_query($con , $query2);

if ($result1->num_rows > 0 && $result2->num_rows > 0 ) {
  
  while($Adata = $result1->fetch_assoc() && $Bdata = $result2->fetch_assoc()) {
 
  echo $Adata['columname'];
  echo $Bdata['columname'];


  }
 else {
  echo "0 results";
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM