【问题标题】:WordPress wpdb trying to select data DISTINCT yet still display duplicated resultsWordPress wpdb 尝试选择数据 DISTINCT 但仍显示重复的结果
【发布时间】:2017-11-06 12:25:08
【问题描述】:

我有一个使用 WordPress 的 MySQL PHP 代码,其中包括一个下拉列表,系统在该下拉列表中获取数据库并在下拉列表中显示数据,在数据库中有一些重复的行。

我需要选择不重复的数据,所以我使用了这个查询,但它不起作用。

 $query_site_location =$wpdb->get_results("select DISTINCT 
                      l.siteID,
                      l.locationName, 
                      l.ownerID, 
                      l.stationID,
                      o.ownerNAME,
                      t.stationName,
                      n.inspectionDate,
                      s.soldierID
                       from army_site_location l
                       LEFT  
                       JOIN owner_site o
                       on l.ownerID = o.ownerID
                       LEFT  
                       JOIN station_type t
                       on l.stationID = t.stationID
                       LEFT  
                       JOIN inspection_info n
                       on l.siteID = n.siteID
                       LEFT  
                       JOIN soldier s
                       on n.soldierID = s.soldierID");

                      foreach($query_site_location as $row)
                      {                                             

echo "<option id = '".$row ->ownerNAME."' name = '".$row ->stationName."' id2 = '".$row ->ownerID."' name2 = '".$row ->stationID."' id3 = '".$row ->soldierName."' name3 = '".$row ->soldierID."' '".$row ->inspectionDate."'  '".$row ->locationName."'>".$row->locationName."</option>";

【问题讨论】:

  • 恕我直言,it didn't work 作为 Stack Overflow 上的问题陈述毫无用处。这可能意味着从“我的服务器起火”到“即使我认为我不应该得到重复的结果”,请edit您的问题更具体。

标签: php mysql wordpress distinct


【解决方案1】:

这就是答案:

 $query_site_location =$wpdb->get_results("Select DISTINCT
                      l.locationName,
                      e.soldierID,
                      e.soldierName,
                      i.inspectionID,
                      i.inspectionDate
                      FROM
                      army_site_location l 
                      JOIN inspection_site s
                      ON
                      s.siteID = l.siteID
                     JOIN inspection_info i 
                     ON
                     i.inspectionID = s.inspectionID
                     JOIN soldier e 
                     ON
                     e.soldierID = i.soldierID");

                     echo $query_site_location;

                      foreach($query_site_location as $row)
                      {




                       // echo "<option id = '".$row ->ownerNAME."' name = '".$row ->stationName."' id2 = '".$row ->ownerID."' name2 = '".$row ->stationID."' '".$row ->locationName."'>".$row->locationName."</option>";

echo "<option id = '".$row ->soldierName."' name = '".$row ->inspectionDate."' id2 = '".$row ->soldierID."' name2 = '".$row ->inspectionID."' '".$row ->locationName."'>".$row->locationName."</option>";



                      } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2020-11-25
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多