只能从我的SELECT语句中获取4个ID

时间:2018-01-26 15:13:02

标签: php mysql select foreach

在我的查询中,我将通过6个表来获取所有6个表中的信息。该查询确实有效,但我只返回前4个ID以及与这些ID相关联的信息。有人可以看到我的查询有什么问题吗?

PS:我正在通过foreach循环查询,所以所有信息都会显示在一张漂亮的表格中。

SELECT v.klantNummer,
       v.contactnaam,
       v.telefoon,
       v.adres,
       v.postcode,
       mk.naam,
       sm.CSnummer,
       ol.onderhoudsDatum
FROM `vestiging` AS v,
     systeem AS s,
     systeemContract AS sc,
     onderhoudsLog AS ol,
     systeemMeldkamer AS sm,
     meldkamers AS mk
WHERE sm.id = s.id
  AND mk.id = s.id
  AND v.id = s.vestigingID
  AND s.id = sc.systeemID
  AND sc.id = ol.systeemContractID

这是我的预言:

foreach($DB->SELECT->onderhoudsGegevens() as $row) {
?>
  <tr>
    <td><?= $row['klantNummer'] ?></td>
    <td><?= $row['contactnaam'] ?></td>
    <td><?= $row['telefoon'] ?></td>
    <td><?= $row['adres'] ?></td>
    <td><?= $row['postcode'] ?></td>
    <td><?= $row['naam'] ?></td>
    <td><?= $row['CSnummer'] ?></td>
    <td><?= datetimeToDate($row['onderhoudsDatum']) ?></td>
  </tr>
   <?php
}
?>

1 个答案:

答案 0 :(得分:1)

这里使用连接使用更传统的表单重写了您的查询。如果你的一个或多个 表没有满足条件的行,您可以将它们各自的连接更改为LEFT JOIN或OUTER JOIN。

SELECT 
    v.klantNummer, 
    v.contactnaam, 
    v.telefoon, 
    v.adres, 
    v.postcode, 
    mk.naam, 
    sm.CSnummer, 
    ol.onderhoudsDatum 
FROM `vestiging` AS v
JOIN systeem AS s
    ON v.id = s.vestigingID
JOIN systeemContract AS sc
    ON s.id = sc.systeemID
JOIN onderhoudsLog AS ol
    ON sc.id = ol.systeemContractID
JOIN systeemMeldkamer AS sm
    ON sm.id = s.id
JOIN meldkamers AS mk
    ON mk.id = s.id

例如,如果表格meldlamers是限制结果的表格,您可以尝试:

SELECT 
    v.klantNummer, 
    v.contactnaam, 
    v.telefoon, 
    v.adres, 
    v.postcode, 
    mk.naam, 
    sm.CSnummer, 
    ol.onderhoudsDatum 
FROM `vestiging` AS v
JOIN systeem AS s
    ON v.id = s.vestigingID
JOIN systeemContract AS sc
    ON s.id = sc.systeemID
JOIN onderhoudsLog AS ol
    ON sc.id = ol.systeemContractID
JOIN systeemMeldkamer AS sm
    ON sm.id = s.id
LEFT JOIN meldkamers AS mk
    ON mk.id = s.id

尝试以上查询,看看是否有更多行。如果没有,请将LEFT添加到其他每个(一次一个),直到您看到更多行。