如何在SQL / PHP中加入多选

时间:2017-10-27 18:05:24

标签: php sql

还没有找到一个好的答案。我需要添加什么来运行下面的两个select语句。我尝试了Union ALL和其他一些东西,但它没有返回数据。任何帮助,将不胜感激。 试着用桌子做这件事。 New table example

    $sql = "select
         bin_loc as [Yard Location],
         status as [Status],
         item_no as [Equipment #]
         from location WHERE type like 'YARD'";

   $sql = "select
         item_no as [Equipment],
         status as [Status],
         bin_loc as [Yard Location]
         from location WHERE type like 'DOCK'";


 $stmt = $db->query($sql);
 $result = $stmt->fetchAll();
 foreach ($result as $row) {

2 个答案:

答案 0 :(得分:0)

您发布的SQL语句不是有效的SQL,这就是它不返回任何数据的原因。如果生成的查询有效,UNION ALL应该可以工作。列名必须匹配才能使UNION正常工作。

这应该返回您要查找的数据:

SELECT 
  bin_loc as [Yard Location],
  status as [Status],
  item_no as [Equipment]
FROM location 
WHERE type like 'YARD'

UNION ALL

SELECT
  bin_loc as [Yard Location],
  status as [Status],
  item_no as [Equipment]
FROM location 
WHERE type like 'DOCK'

答案 1 :(得分:0)

我最终创建了2个表来执行我需要的操作,而不是尝试在一个表中完成所有操作,因为这些字段需要按照特定的顺序进行演示。包括像我这样的新手的php和SQL。 谢谢你的帮助。

 1st table:
                     <tbody>
                                <?php 
                                    $database = new Connection();
                                    $db = $database->openConnection();
                                    $sql = "select
                                                bin_loc as [Yard Location],
                                                status as [Status],
                                                item_no as [Equipment#]
                                                from location
                                                WHERE type like 'YARD'";
                                    $stmt = $db->query($sql);
                                    $result = $stmt->fetchAll();
                                    foreach ($result as $row) {
                                ?>
                                        <tr class="gradeX">
                                            <td><?= $row["Yard Location"</td>
                                            <td><?= $row["Status"] ?></td>
                                            <td><?= $row["Equipment#"] ?</td>
                                         </tr>                    
                               <?php
                                    } 
                               ?>
                                </tbody>

Second table
                   <tbody>
                                <?php
                                    $sql = "select
                                                item_no as [Equipment],
                                                status as [Status],
                                                bin_loc as [Dock Location]
                                                from location
                                                WHERE type like 'DOCK'";
                                    $stmt = $db->query($sql);
                                    $result = $stmt->fetchAll();
                                    foreach ($result as $row) {
                                ?>
                                        <tr class="gradeX">
                                            <td><?= $row["Equipment"] ?></td>
                                            <td><?= $row["Status"] ?></td>
                                            <td><?= $row["Dock Location"</td>
                                         </tr>                       
                               <?php
                                    } 
                                    $database->closeConnection();
                               ?>
                                </tbody>