php 使用SQL内部连接从两个表中获取数据,尝试获取非对象的属性

x33g5p2x  于 11个月前  发布在  PHP
关注(0)|答案(2)|浏览(113)

我有两个表事件和会话,如下所示
事件表


的数据
会话表le

以下是预期结果



这是我的解决方案

<table>
                  <tr>
                    <th>Sessions </th>
                    <th>konto</th>
                    <th>Mobile</th>
                    <th>Komputer</th>
                    <th>Date</th>
                  </tr>       
                  <?php
                      $conn = mysqli_connect("localhost", "root", "", "ideabank_julia");
                      // Check connection
                      if ($conn->connect_error) {
                      die("Connection failed: " . $conn->connect_error);
                      }
                      $sql = "SELECT sid, datetime, count(*) as num_rows, count(distinct sid) as sessions,  
                      sum( targetbuttonname = 'konto' ) as num_konto,
                      sum(devicetype ='Computer') as num_computer, 
                      sum(devicetype = 'Mobile') as num_mobile from events
                      INNER JOIN sessions ON events.sid = sessions.sid group by sid, datetime;";

                      $result = $conn->query($sql);
                      if ($result->num_rows > 0) {
                      // output data of each row
                      while($row = $result->fetch_assoc()) {
                      echo "<tr>
                      <td>". $row["num_rows"]."</td>
                      <td>". $row["num_konto"]."</td>         
                      <td>". $row["num_mobile"]. "</td>
                      <td>". $row["num_computer"]. "</td>
                      <td>". $row["datetime"]. "</td>

                      </tr>";
                      }
                      echo "</table>";
                      } else { echo "0 results"; }
                      $conn->close();
                      ?>
                </table>

字符串
不幸的是,当我在phpmyadmin上运行脚本时,我得到了以下错误

# 1052 - Column: 'sid' in field list is ambiguous


当我运行上面的php脚本时,我得到以下错误

Trying to get property of non-object


我在代码中做错了什么?

bzzcjhmw

bzzcjhmw1#

我认为你应该添加别名到您所选择的列,以说明你是从哪个表中采取的列。请尝试下面的代码。

$sql = "SELECT events.sid, events.datetime, count(events.*) as num_rows, count(distinct events.sid) as sessions,  
                  sum( events.targetbuttonname = 'konto' ) as num_konto,
                  sum(events.devicetype ='Computer') as num_computer, 
                  sum(events.devicetype = 'Mobile') as num_mobile from events
                  INNER JOIN sessions ON events.sid = sessions.sid group by sid, datetime";

字符串

0qx6xfy6

0qx6xfy62#

您应该为groupby条件中的列添加别名。

$sql = "SELECT even.sid, even.datetime, COUNT(*) AS num_rows, 
                 COUNT(distinct even.sid) AS sessions,  
                 SUM(even.targetbuttonname = 'konto') AS num_konto,
                 SUM(devicetype = 'Computer') AS num_computer, 
                 SUM(devicetype = 'Mobile') AS num_mobile 
          FROM events even
          INNER JOIN sessions ON even.sid = sessions.sid 
          GROUP BY even.sid, even.datetime";

字符串

相关问题