Ich habe 3 verwandte Tabellen in meiner Datenbank und ich versuche, gefilterte insgesamt angegebene Spalten für das aktuelle Datum zurückzugeben. Qty [sum (pdr_qty)], Produktion qty [sum (pg_qty)]. >
Abschnitt < /th>
Defekte Qty < /th>
Produktion qty < /th> < Br /> < /tr>
< /thead>
Zeile 2 < /td>
100
300
Tabelle 1: Abschnitte < /p>
sec_id < /th>
sec_name < /th>
< /tr>
< /thead>
< Tr>
1 < /td>
Zeile 1 < /td>
< /tr>
2
Zeile 2
Table2: production_defects_report
pdr_id < /th>
pdr_section_id < /th>
pdr_date < /th>
pdr_qty < /th>
< /tr>
< /thead>
1 < /td>
2 < /td >
2023-12-10 16:25:46 < /td>
50 < /td>
< /tr>
< Br /> 2 < /td>
2 < /td>
2023-12-10 16:25:47 < /td>
50
Tabelle 3: production_gate
/>
pg_id < /th>
pg_sec_id < /th>
pg_date < /th>
pg_qty < /th>
< /tr>
< / thead>
1 < /td>
2 < /td>
2023-12 -10 16:25:46 < /td>
100 < /td>
< /tr>
2 < /td>
2 < /td>
2023-12-10 16:25:47 < /td>
200 < /td>
< /tr>
< /tbody>
< /table> < /div>
Meine Modellmethode: < /p>
public function fetch_data($limit, $start)
{
$today = date('Y-m-d');
$this->db->select('sections.sec_name, SUM(production_defects_report.pdr_qty) as defect_qty, SUM(production_gate.pg_qty) as production_qty');
$this->db->from('production_defects_report');
$this->db->join('sections', 'sections.sec_id = production_defects_report.pdr_section_id');
$this->db->join('production_gate', 'production_gate.pg_sec_id = production_defects_report.pdr_section_id ', 'left');
$this->db->where('date(production_defects_report.pdr_date)',$today);
$this->db->where('date(production_gate.pg_date)',$today);
$this->db->group_by('sections.sec_id');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
< /code>
Ich habe eine Modellmethode unter Verwendung von CodeIgniter3 erstellt, aber ich habe festgestellt, dass die gruppierten Summen falsch berechnet werden-sie sind überlastend. Das Problem scheint das Ergebnis des Zusammenfassen eines kartesischen Produkts zu sein. Mit anderen Worten, die Join -Klauseln führen dazu, dass qualifizierte Zeilen innerhalb von Gruppen dupliziert werden, und die Summe () -Werte sind unbrauchbar.
SQL-Abfragebruppen gruppierten Datensätze aus zwei zusammengefügten Tabellen sind überlagende Mengen ⇐ Php
-
- Similar Topics
- Replies
- Views
- Last post