Author Topic: LEFT JOIN in the query did not work properly  (Read 9056 times)

0 Members and 1 Guest are viewing this topic.

Offline Sun Zaza

  • Sr. Member
  • ****
  • Posts: 399
    • View Profile
LEFT JOIN in the query did not work properly
« on: January 13, 2009, 05:29:23 PM »
Hello,

I really need some support to correct this query for me.

I added 2 tables to the 4images database.

TEAMS (teams_id, teams_name,....)
 &
RESULTS (results_id, results_home, results_out, .....)

In my additional page results.php I have this information:

Barcelona vs Dynamo Moscow 5 - 5

The problem is that I cannot get the value of the teams_id (When you click on the name the team)


I am using this query, but I still not have the teams_id:


Code: [Select]
$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 (".TEAMS_TABLE." t)
LEFT JOIN ".RESULTS_TABLE." r ON r.results_home = t.teams_name
   
GROUP BY r.results_id
ORDER BY r.results_id ASC";
       
$result = $site_db->query($sql);
$num_rows = $site_db->get_numrows($result);


I really tries everything, but.... :cry:

I believe there is something wrong with the LEFT JOIN.



Offline KurtW

  • 4images Guru
  • *******
  • Posts: 2.778
    • View Profile
    • Malediven-Bilder ~~Dreams~~
Re: LEFT JOIN in the query did not work properly
« Reply #1 on: January 13, 2009, 06:04:08 PM »
hi,

a lot is wrong.
more codes are possible... this is one:
Code: [Select]
$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);

Kurt

Offline Sun Zaza

  • Sr. Member
  • ****
  • Posts: 399
    • View Profile
Re: LEFT JOIN in the query did not work properly
« Reply #2 on: January 13, 2009, 06:19:32 PM »
Thanks KurtW,

I still not get the teams_id.

I wonder what wrong in this code.
« Last Edit: January 13, 2009, 08:13:43 PM by cruxy »

Offline Sun Zaza

  • Sr. Member
  • ****
  • Posts: 399
    • View Profile
Re: LEFT JOIN in the query did not work properly
« Reply #3 on: January 14, 2009, 12:08:07 AM »
Can iemand correct this query for me please?

I spend de hole day trying solving this issue without any success.

I really need your PHP knowledge.

Many thanks in advance,

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: LEFT JOIN in the query did not work properly
« Reply #4 on: January 14, 2009, 03:01:25 AM »
Maybe if you do export in phpmyadmin of these two tables (structure + data) and post the structure + 1 data line for each table we could have better understanding the relationship between these two tables.
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline Sun Zaza

  • Sr. Member
  • ****
  • Posts: 399
    • View Profile
Re: LEFT JOIN in the query did not work properly
« Reply #5 on: January 14, 2009, 09:11:28 AM »
Thanks V@no for your reaction.

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.

More explanation:

The value of the field uitslagen_thuis (the teams hoe is playing at home) & uitslagen_uit (From the UITSLAGEN TABLE (RESULTS), you can find it always in the TEAMS TABLE.

I do know how it works in MySql, but in Microsoft Excel I do like this: Find uitslagen_thuis (From the  UITSLAGEN TABLE in the TEAMS TABLE and then give me the teams_id (VLookup function).
NB: I just want to be clear.


Here you are:

Code: [Select]
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);

Please let me know if I have to change or add something from those two tables. I need to solve this issue.


Many thanks in advance V@no :)
« Last Edit: January 14, 2009, 02:55:12 PM by cruxy »

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: LEFT JOIN in the query did not work properly
« Reply #6 on: January 14, 2009, 09:37:09 AM »
unless I missunderstood what is the problem, but using your original query it returns teams_id just fine.
Code: [Select]
<?php
define
('ROOT_PATH''./');
include(
ROOT_PATH.'global.php');
$sql "SELECT r.uitslagen_id, r.uitslagen_thuis, r.uitslagen_uit, r.uitslagen_doelpunten_thuis, r.uitslagen_doelpunten_uit, r.uitslagen_done, t.teams_id, t.teams_name
FROM (
4images_teams t
)
LEFT JOIN 4images_uitslagen r ON r.uitslagen_thuis = t.teams_name
GROUP BY r.uitslagen_id
ORDER BY r.uitslagen_id ASC"
;

$result $site_db->query($sql);
echo 
"<pre>";
while(
$row $site_db->fetch_array($result))
{
  
print_r($row);
}
?>

The result displayed:
Quote
Array
(
   
  • =>
[uitslagen_id] =>
    [1] =>
    [uitslagen_thuis] =>
    [2] =>
    [uitslagen_uit] =>
    [3] =>
    [uitslagen_doelpunten_thuis] =>
    [4] =>
    [uitslagen_doelpunten_uit] =>
    [5] =>
    [uitslagen_done] =>
    [6] => 2
    [teams_id] => 2
    [7] => Real Madrid
    [teams_name] => Real Madrid
)
Array
(
   
  • => 1
[uitslagen_id] => 1
    [1] => Barcelona
    [uitslagen_thuis] => Barcelona
    [2] => Real Madrid
    [uitslagen_uit] => Real Madrid
    [3] => 5
    [uitslagen_doelpunten_thuis] => 5
    [4] => 5
    [uitslagen_doelpunten_uit] => 5
    [5] => 0
    [uitslagen_done] => 0
    [6] => 1
    [teams_id] => 1
    [7] => Barcelona
    [teams_name] => Barcelona
)
Array
(
   
  • => 2
[uitslagen_id] => 2
    [1] => Barcelona
    [uitslagen_thuis] => Barcelona
    [2] => Real Madrid
    [uitslagen_uit] => Real Madrid
    [3] => 4
    [uitslagen_doelpunten_thuis] => 4
    [4] => 4
    [uitslagen_doelpunten_uit] => 4
    [5] => 1
    [uitslagen_done] => 1
    [6] => 1
    [teams_id] => 1
    [7] => Barcelona
    [teams_name] => Barcelona
)
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline Sun Zaza

  • Sr. Member
  • ****
  • Posts: 399
    • View Profile
Re: LEFT JOIN in the query did not work properly
« Reply #7 on: January 14, 2009, 02:22:18 PM »
You are right V@no. I just tested on a new php file and I get the good results. Very strange.

To be sure I give you here the code of the php page uitslagen.php. Maybe you can see what I did wrong:

Code: [Select]
<?php
/**************************************************************************
 *                                                                        *
 *    4images - A Web Based Image Gallery Management System               *
 *    ----------------------------------------------------------------    *
 *                                                                        *
 *             File: uitslagen.php                                          *
 *        Copyright: (C) 2002 Jan Sorgalla                                *
 *            Email: jan@4homepages.de                                    *
 *              Web: http://www.4homepages.de                             *
 *    Scriptversion: 1.7.6                                                *
 *                                                                        *
 *    Never released without support from: Nicky (http://www.nicky.net)   *
 *                                                                        *
 **************************************************************************
 *                                                                        *
 *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
 *    bedingungen (Lizenz.txt) für weitere Informationen.                 *
 *    ---------------------------------------------------------------     *
 *    This script is NOT freeware! Please read the Copyright Notice       *
 *    (Licence.txt) for further information.                              *
 *                                                                        *
 *************************************************************************/
 
$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)
GROUP BY u.uitslagen_id
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>teams ID</b></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)){
  
  
  
   
$teams_id $teams_row['teams_id'];
   
$teams_name $teams_row['teams_name'];
   
$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>"
.$teams_id."</td>
   <td><a href=\""
.$site_sess->url(ROOT_PATH."teams.php?teams_id=".$teams_row['teams_id'])."\" target=\"_blank\">".$uitslagen_row['uitslagen_thuis']."</a></td>
   <td><a href=\""
.$site_sess->url(ROOT_PATH."teams.php?teams_id=".$teams_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."evenementen.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');
?>

en the test page retuns this results:

Quote
Array
(
   
  • => 4
[uitslagen_id] => 4
    [1] => Barcelona
    [uitslagen_thuis] => Barcelona
    [2] => Real Madrid
    [uitslagen_uit] => Real Madrid
    [3] => 5
    [uitslagen_doelpunten_thuis] => 5
    [4] => 5
    [uitslagen_doelpunten_uit] => 5
    [5] => 1
    [uitslagen_done] => 1
    [6] => 1
    [teams_id] => 1
    [7] => Barcelona
    [teams_name] => Barcelona
)
Array
(
   
  • => 5
[uitslagen_id] => 5
    [1] => Real Madrid
    [uitslagen_thuis] => Real Madrid
    [2] => Barcelona
    [uitslagen_uit] => Barcelona
    [3] => 4
    [uitslagen_doelpunten_thuis] => 4
    [4] => 4
    [uitslagen_doelpunten_uit] => 4
    [5] => 1
    [uitslagen_done] => 1
    [6] => 2
    [teams_id] => 2
    [7] => Real Madrid
    [teams_name] => Real Madrid
)



See also attachment please.

I hope you can help me to solve this mysterious issue.

« Last Edit: January 14, 2009, 02:40:09 PM by cruxy »

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: LEFT JOIN in the query did not work properly
« Reply #8 on: January 14, 2009, 03:58:43 PM »
you are using unexisting variable $teams_row
use this instead:
Code: [Select]
   $teams_id = $uitslagen_row['teams_id'];
   $teams_name = $uitslagen_row['teams_name'];


A tip: when you do custom modifications, you should have error_reporting(E_ALL), so it would show warnings and other useful messages for debugging if you are using undefined variables, etc ;)
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline Sun Zaza

  • Sr. Member
  • ****
  • Posts: 399
    • View Profile
Re: LEFT JOIN in the query did not work properly
« Reply #9 on: January 14, 2009, 04:07:53 PM »
Perfect V@no :D I don't believe my eyes.

We still have a small problem.

The uitslagen_uit get the wrong teams_id. I believe we have to change something in the querey.

I tried this, but without success:

Code: [Select]
        FROM ".TEAMS_TABLE." t
LEFT JOIN ".UITSLAGEN_TABLE." u ON (u.uitslagen_thuis = t.teams_name OR u.uitslagen_uit = t.teams_name)

Is that correct?

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: LEFT JOIN in the query did not work properly
« Reply #10 on: January 14, 2009, 04:22:45 PM »
yes, that looks ok, however as KurtW suggested, you should left join teams table instead ;)
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline Sun Zaza

  • Sr. Member
  • ****
  • Posts: 399
    • View Profile
Re: LEFT JOIN in the query did not work properly
« Reply #11 on: January 14, 2009, 06:50:33 PM »
Hi V@no,

Thanks to your help I solved it with this query:

Code: [Select]
$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";

Thank you very much V@no.
PS: My thanks goes also to KurtW.



Offline V@nо

  • Addicted member
  • ******
  • Posts: 1.223
    • View Profile
Re: LEFT JOIN in the query did not work properly
« Reply #12 on: January 14, 2009, 07:23:18 PM »
but I thought it worked when you were using one left join and "or"
Your first three "must do" before you ask a question:
If I asked you to PM me, I meant PM to my primary account, this account doesn't accept PMs.

Offline Sun Zaza

  • Sr. Member
  • ****
  • Posts: 399
    • View Profile
Re: LEFT JOIN in the query did not work properly
« Reply #13 on: January 14, 2009, 07:26:31 PM »
No. It did not. The home & the out teams had the same teams ID "1".