Jump to content

[SOLVED] LEFT JOIN did not work for me


cruxy

Recommended Posts

Hello,

 

I have a problem and I don't know how to solve. I hope I get some support from you.

 

I have tow tables:

 

Table TEAMS:

teams_id

teams_name

 

Table Results:

results_id

results_home

results_out

 

 

In the page results.php I am using this query:

 

$sql = "SELECT r.results_id, r.results_home, r.results_out, r.results_goals_home, r.results_goals_out, r.results_done, t.teams_id, t.teams_name
        FROM ".RESULT_TABLE." r
                  LEFT JOIN ".TEAMS_TABLE." t ON (t.team_name = r.results_home)
	GROUP BY r.results_id
	ORDER BY r.results_id ASC";
        
$result = $site_db->query($sql); 
$num_rows = $site_db->get_numrows($result);

 

This query returns this line:

 

Barcelona vs Milan 5 X 5 (Soccer game)

 

The problem is that I the query cannot returns the teams_id.

That means I want when you click on the name of the club, you go to page teams.php?teams_id=** (** = teams_id of the team)

 

 

I need some help.

Link to comment
https://forums.phpfreaks.com/topic/140723-solved-left-join-did-not-work-for-me/
Share on other sites

Thanks fenway. I just removed it, but nothing it changed.

The query still not returning the teams_id.

 

This is the info of my database:

 

Tabel structuur voor tabel `4images_teams`
--

CREATE TABLE IF NOT EXISTS `4images_teams` (
  `teams_id` int(10) NOT NULL auto_increment,
  `teams_name` varchar(255) NOT NULL,
  `teams_description` text NOT NULL,
  `teams_gespeeld` int(2) NOT NULL default '0',
  `teams_gewonnen` int(2) NOT NULL default '0',
  `teams_gelijk` int(2) NOT NULL default '0',
  `teams_verloren` int(2) NOT NULL default '0',
  `teams_punten` int(2) NOT NULL default '0',
  `teams_doelpunten_voor` int(3) NOT NULL default '0',
  `teams_doelpunten_tegen` int(3) NOT NULL default '0',
  `teams_punten_mindering` int(2) NOT NULL default '0',
  `teams_zaal` varchar(50) NOT NULL default '',
  `teams_voorkeurstijd` varchar(5) NOT NULL default '',
  `teams_clubkleuren` varchar(25) NOT NULL default '',
  PRIMARY KEY  (`teams_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
-- Gegevens worden uitgevoerd voor tabel `4images_teams`
--

INSERT INTO `4images_teams` (`teams_id`, `teams_name`, `teams_description`, `teams_gespeeld`, `teams_gewonnen`, `teams_gelijk`, `teams_verloren`, `teams_punten`, `teams_doelpunten_voor`, `teams_doelpunten_tegen`, `teams_punten_mindering`, `teams_zaal`, `teams_voorkeurstijd`, `teams_clubkleuren`) VALUES
(1, 'Barcelona', '', 3, 0, 3, 0, 3, 14, 14, 0, '', '', ''),
(2, 'Real Madrid', '', 3, 0, 3, 0, 3, 14, 14, 0, '', '', '');

-- --------------------------------------------------------

--
-- Tabel structuur voor tabel `4images_uitslagen`
--

CREATE TABLE IF NOT EXISTS `4images_uitslagen` (
  `uitslagen_id` int(3) NOT NULL auto_increment,
  `uitslagen_datum` int(10) default NULL,
  `uitslagen_speelronde` int(2) NOT NULL default '0',
  `uitslagen_thuis` varchar(25) NOT NULL default '',
  `uitslagen_uit` varchar(25) NOT NULL,
  `uitslagen_doelpunten_thuis` int(2) NOT NULL,
  `uitslagen_doelpunten_uit` int(2) NOT NULL,
  `uitslagen_done` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`uitslagen_id`),
  KEY `uitslagen_thuis` (`uitslagen_thuis`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
-- Gegevens worden uitgevoerd voor tabel `4images_uitslagen`
--

INSERT INTO `4images_uitslagen` (`uitslagen_id`, `uitslagen_datum`, `uitslagen_speelronde`, `uitslagen_thuis`, `uitslagen_uit`, `uitslagen_doelpunten_thuis`, `uitslagen_doelpunten_uit`, `uitslagen_done`, `uitslagen_thuis_id`, `uitslagen_uit_id`) VALUES
(1, 1231912800, 1, 'Barcelona', 'Real Madrid', 5, 5, 0, 0, 0),
(2, 1231912800, 1, 'Barcelona', 'Real Madrid', 4, 4, 1, 0, 0);

 

I hope someone can help me.

 

See also the screenshot to understand more the situation.

 

Many thanks in advance,

Cruxy

 

 

[attachment deleted by admin]

Thank you fenway for trying helping me in this issue.

 

I already solved 50% of this problem, because I can now get the teams_id of the home team ($uitslagen_thuis), but the out team ($uitslagen_uit) get the teams_id of the home team.

 

Some fields are in Dutch (I am from the Netherlands).

 

uitslagen = results

thuis = home

uit = out

doelpunten = goals

 

The rest is not important I believe.

 

 

<?php
$main_template = 'uitslagen';


define('GET_CACHES', 1);
define('ROOT_PATH', './');
include(ROOT_PATH.'global.php');
require(ROOT_PATH.'includes/sessions.php');
include(ROOT_PATH.'includes/page_header.php');



//-----------------------------------------Start uitslagen--------------------------------------------------------------------

$sql = "SELECT u.uitslagen_id, u.uitslagen_thuis, u.uitslagen_uit, u.uitslagen_doelpunten_thuis, u.uitslagen_doelpunten_uit, u.uitslagen_done, t.teams_id, t.teams_name



        FROM ".TEAMS_TABLE." t 
	LEFT JOIN ".UITSLAGEN_TABLE." u ON (u.uitslagen_thuis = t.teams_name)
	ORDER BY u.uitslagen_id ASC";
        
$result = $site_db->query($sql); 
$num_rows = $site_db->get_numrows($result);

if (!$num_rows)  { 
   $uitslagen = "Geen uitslagen"; 
} 
else  {
    $uitslagen = "<table class=\"head2000\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"4\">
    <tr>
    <td>#</td>
    <td><b>Club1</b></td>
    <td><b>Club2</b></td>
    <td><b>Uitslagen</b></td>
    <td><b>Uitslagen</b></td>
    </tr>
    ";
   
   
   $i = 1;
   while ($uitslagen_row = $site_db->fetch_array($result)){

   $uitslagen_thuis = $uitslagen_row['uitslagen_thuis'];
   $uitslagen_uit = $uitslagen_row['uitslagen_uit'];
   $uitslagen_done = $uitslagen_row['uitslagen_done'];
   
   
   
//-------------------------------------------------   
   if ($uitslagen_done == 0)
   {
   $uitslagen_doelpunten_thuis = '-';
   $uitslagen_doelpunten_uit = '-';
   }   
   else {	
	$uitslagen_doelpunten_thuis = $uitslagen_row['uitslagen_doelpunten_thuis'];	
	$uitslagen_doelpunten_uit = $uitslagen_row['uitslagen_doelpunten_uit'];
   }
//-------------------------------------------------   

   
  $uitslagen .= "<tr>
  <td>".$i++."</td>
  <td><a href=\"".$site_sess->url(ROOT_PATH."teams.php?teams_id=".$uitslagen_row['teams_id'])."\" target=\"_blank\">".$uitslagen_row['uitslagen_thuis']."</a></td>
  <td><a href=\"".$site_sess->url(ROOT_PATH."teams.php?teams_id=".$uitslagen_row['teams_id'])."\" target=\"_blank\">".$uitslagen_row['uitslagen_uit']."</a></td>
  <td>".$uitslagen_doelpunten_thuis."</td>
  <td>".$uitslagen_doelpunten_uit."</td>
  </tr>

   ";                   
}      
} 
  //$uitslagen .= "</table>\n";
  
$site_template->register_vars("uitslagen", $uitslagen); 
unset($uitslagen);
//----------------------------------------Start uitslagen------------------------------------



//-----------------------------------------------------
//--- Clickstream -------------------------------------
//-----------------------------------------------------

$clickstream = "<span class=\"clickstream\"><a href=\"".$site_sess->url(ROOT_PATH."index.php")."\" class=\"clickstream\">".$lang['home']."</a>".$config['category_separator']."<a href=\"".$site_sess->url(ROOT_PATH."uitslagen.php")."\" class=\"clickstream\">uitslagen pagina</a>".$config['category_separator'].$eventsROWname."</span>";



//-----------------------------------------------------
//--- Print Out ---------------------------------------
//-----------------------------------------------------
$site_template->register_vars(array(
  "clickstream" => $clickstream,
  "teams_name" => $teams_name,
  "teams_id" => $teams_id,
  "teams_description" => $teams_description,
  //"image_name" => format_text($image_row['image_name'], 2),

));
$site_template->print_template($site_template->parse_template($main_template));

include(ROOT_PATH.'includes/page_footer.php');
?>

 

I hope you can help me solving this issue.

 

[attachment deleted by admin]

Hi,

 

I solved it with this query:

 

$sql = "SELECT u.uitslagen_id, u.uitslagen_thuis, u.uitslagen_uit, u.uitslagen_doelpunten_thuis, u.uitslagen_doelpunten_uit, u.uitslagen_done, t.teams_id, t.teams_name, tu.teams_name, tu.teams_id AS teams_id2

        FROM ".UITSLAGEN_TABLE." u		
	LEFT JOIN ".TEAMS_TABLE." t ON (u.uitslagen_thuis = t.teams_name)	
	LEFT JOIN ".TEAMS_TABLE." tu ON (u.uitslagen_uit = tu.teams_name)		
	ORDER BY u.uitslagen_id ASC";

 

Thanks anyway for your help :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.