我做了一个查询,只选择每个用户的最后日期。它可以在phpmyadmin中运行,但是当我想在PHP中的mysqli_query()中执行它时,它不会返回任何内容,甚至不会出错。
代码是:
select * from table t inner join ( select User_ID, max(Date) as MaxDate from table group by User_ID ) tm on t.User_ID = tm.User_ID and t.Date = tm.MaxDate
如果您有任何想法请告诉我:)
EDIT PHP代码是:
$id = $_SESSION["ID"];
$SqlQuery = "SELECT * from 'tablename' t inner join ( select 'User_ID', max('Date') as 'MaxDate' from 'tablename' group by 'User_ID' ) tm on 't.User_ID' = 'tm.User_ID' and 't.Date' = 'tm.MaxDate'";
$Result = mysqli_query($link, $SqlQuery) or die ("not possible to execute query: $sql on $link");
if ($Result->num_rows > 0) {
while($row = $Result->fetch_assoc()) {
echo "<br> id: ". $row['Sick_ID']. " - UserID: ". $row['User_ID']. "- Reason " . $row['Reason'] . "<br>";
}
} else {
echo "0 results";
}
答案 0 :(得分:2)
将架构所有者前缀添加到选择查询。它通常发生在PHP上执行mysql查询时。
Select * from data.table_name as t1 inner join data.table_name_2 as t2 .....
如果更好:
Select data.t1.id, data.t2.name from data_table_name as t1 .....