141
« on: January 22, 2009, 09:48:11 PM »
Thanks V@no for your reply. I tried your query many times, but without success.
I will give you a working code, so maybe you get a better idea about what I am looking for.
To return the HOME information (uitslagen_thuis_streak = info home) of that team ($teams_id = Barcelona) I used this query:
$sql = "SELECT u.uitslagen_thuis_streak, u.uitslagen_uit_streak, u.uitslagen_thuis, u.uitslagen_uit, u.uitslagen_id, tt.teams_id, tu.teams_id
FROM ".UITSLAGEN_TABLE." u
LEFT JOIN ".TEAMS_TABLE." tt ON (u.uitslagen_thuis = tt.teams_name)
LEFT JOIN ".TEAMS_TABLE." tu ON (u.uitslagen_uit = tu.teams_name)
WHERE $teams_id = tt.teams_id
ORDER BY u.uitslagen_datum ASC
";
$result = $site_db->query($sql);
$num_rows = $site_db->get_numrows($result);
while ($uitslagen_row = $site_db->fetch_array($result)) {
$streak .= "".$uitslagen_row['uitslagen_thuis_streak']."<BR/>";
}
$site_template->register_vars("streak", $streak);
The result is:
W
V
To return the AWAY information (uitslagen_uit_streak = Info away) of that team ($teams_id = Barcelona) I used this query:
$sql = "SELECT u.uitslagen_thuis_streak, u.uitslagen_uit_streak, u.uitslagen_thuis, u.uitslagen_uit, u.uitslagen_id, tt.teams_id, tu.teams_id
FROM ".UITSLAGEN_TABLE." u
LEFT JOIN ".TEAMS_TABLE." tt ON (u.uitslagen_thuis = tt.teams_name)
LEFT JOIN ".TEAMS_TABLE." tu ON (u.uitslagen_uit = tu.teams_name)
WHERE $teams_id = tu.teams_id
ORDER BY u.uitslagen_datum ASC
";
$result = $site_db->query($sql);
$num_rows = $site_db->get_numrows($result);
while ($uitslagen_row = $site_db->fetch_array($result)) {
$streak .= "".$uitslagen_row['uitslagen_uit_streak']."<BR/>";
}
$site_template->register_vars("streak", $streak);
The result is:
G
Now I want to combine those two query in one query.
I need to return the result like this:
W
G
V
I already solved it with PHP, but I need the query table to use in other query (FROM (SQL 2) A ....)
Check the PHP solution:
$sql = "SELECT u.uitslagen_thuis_streak, u.uitslagen_uit_streak, u.uitslagen_thuis, u.uitslagen_uit, u.uitslagen_id, u.uitslagen_datum, uitslagen_speelronde, tt.teams_id, tu.teams_id, tt.teams_name AS teamsTHUIS, tu.teams_name AS teamsUIT
FROM ".UITSLAGEN_TABLE." u
LEFT JOIN ".TEAMS_TABLE." tt ON (u.uitslagen_thuis = tt.teams_name)
LEFT JOIN ".TEAMS_TABLE." tu ON (u.uitslagen_uit = tu.teams_name)
WHERE $teams_id = tt.teams_id OR $teams_id = tu.teams_id
ORDER BY u.uitslagen_datum ASC
";
$result = $site_db->query($sql);
$num_rows = $site_db->get_numrows($result);
while ($uitslagen_row = $site_db->fetch_array($result)) {
if ($uitslagen_row['uitslagen_thuis'] != $teamsNAME AND $uitslagen_row['uitslagen_thuis_streak'] == 'W')
{
$streak_teams = 'V';
}
elseif ($uitslagen_row['uitslagen_thuis'] != $ teamsNAME AND $uitslagen_row['uitslagen_thuis_streak'] == 'V')
{
$streak_teams = 'W';
}
else
{
$streak_teams = $uitslagen_row['uitslagen_thuis_streak'];
}
$streak .= "".$uitslagen_row['uitslagen_speelronde']." (".date("d-m-Y", $uitslagen_row['uitslagen_datum']).") ".$streak_teams." ".$uitslagen_row['uitslagen_thuis']."<BR/>";
}
$site_template->register_vars("streak", $streak);
NB: I already contacted PHPFREAKS.COM and also the biggest PHP forum in the Netherlands, but nobody could code this query yet.
Am I requesting something impossible from MySql?