将 2 SQL 合二为一
Combining 2 SQL's into one
$sql = "SELECT teams.id,teams.name,teams.updated
FROM teams
ORDER BY teams.name ASC";
list($teams) = getrows($sql);
foreach($teams as $team)
{
$sql = "SELECT time FROM events WHERE (home_team='".$team['id']."' OR away_team='".$team['id']."') AND (status='FT' OR status='PEN' OR status='AET') ORDER BY time DESC LIMIT 1";
所以我希望将它们组合成 1 sql 但是我在尝试使用第一个 sql 的可验证对象时遇到问题如果我使用 LEFT JOIN (SELECT * FROM events WHERE team.id='|来自第一个查询的团队|') 并在子查询中包含原始团队 ID。如果说得通。
谢谢
希望我理解正确
SELECT MAX(events.time) AS latest_time, teams.id, teams.name, teams.updated
FROM events
LEFT JOIN teams ON events.home_team=teams.id OR events.away_team=teams.id
WHERE ...
ORDER BY ...
GROUP BY teams.id
分组依据,因为我认为在 table 事件中每个 teams.id 有多行。然后您可以使用 MAX().
提取最高(最新)时间
$sql = "SELECT teams.id,teams.name,teams.updated
FROM teams
ORDER BY teams.name ASC";
list($teams) = getrows($sql);
foreach($teams as $team)
{
$sql = "SELECT time FROM events WHERE (home_team='".$team['id']."' OR away_team='".$team['id']."') AND (status='FT' OR status='PEN' OR status='AET') ORDER BY time DESC LIMIT 1";
所以我希望将它们组合成 1 sql 但是我在尝试使用第一个 sql 的可验证对象时遇到问题如果我使用 LEFT JOIN (SELECT * FROM events WHERE team.id='|来自第一个查询的团队|') 并在子查询中包含原始团队 ID。如果说得通。
谢谢
希望我理解正确
SELECT MAX(events.time) AS latest_time, teams.id, teams.name, teams.updated
FROM events
LEFT JOIN teams ON events.home_team=teams.id OR events.away_team=teams.id
WHERE ...
ORDER BY ...
GROUP BY teams.id
分组依据,因为我认为在 table 事件中每个 teams.id 有多行。然后您可以使用 MAX().
提取最高(最新)时间