I have two table events and session which looks like this
Events table
Here is expected results
Here is my solution
<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>
Unfortunately when i run the script on phpmyadmin I am getting the following error
# 1052 - Column: 'sid' in field list is ambiguous
And when I run on php script above I get the following error
Trying to get property of non-object
What am I doing wrong in my code?
2
Answers
I think you should add alias to your selected columns to mention from which table you are taking the colums. Please Try the code below.
You should add alias name for coloumns in groupby condition.