1
jmass
Multiple Table Joins
  • 2004/2/20 18:25

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


I have a table that has ticket info. ( Ticket Subject, TicketDetails, StatusID, ClientID, PriorityID)

I have other tables that equate the StatusID, ClientID, PriorityID, etc with names for the IDs.

I need to query the DB to retreive all of the ticket data. But instead of the ClientID, StatusID, PiorityID, etc. I would like to have the equated name returned. I know that I need to do a fairly complex table join. But so far no luck with it.

Here is what I tried last:

-------------------------------
function chooseUserTickets( $UID )
{

global $xoopsDB;
$ret = array();
$ticket_table = $xoopsDB->prefix('obsclient_hd_ticketdata');
$client_table = $xoopsDB->prefix('obsclient_clients');
$category_table = $xoopsDB->prefix('obsclient_hd_categories');
$status_table = $xoopsDB->prefix('obsclient_hd_status');
$priority_table = $xoopsDB->prefix('obsclient_hd_priorities');

$sql = "SELECT ticket.TicketID, ticket.EntryDate, ticket.TicketSubject, client.ClientName, category.CategoryName, status.StatusName, prority.PriorityName FROM $ticket_table AS ticket LEFT JOIN $client_table AS client, $category_table AS category, $status_table AS status, $priority_table AS priority ON client.ClientID = ticket.ClientID, category.CategoryID = ticket.CategoryID, status.StatusID = ticket.StatusID, priority.PriorityID = ticket.PriorityID WHERE ticket.UID = $UID";

$result_choose = $xoopsDB->query($sql);
while ( $client_row = $xoopsDB->fetchArray($result_choose) )

{
$ret[] = array('TicketID' => $client_row['TicketID'], 'EntryDate' => $client_row['EntryDate'], 'TicketSubject' => $client_row['TicketSubject'], 'ClientName' => $client_row['ClientName'], 'CategoryName' => $client_row['CategoryName'], 'StatusName' => $client_row['StatusName'], 'PriorityName' => $client_row['PriorityName']);

}
return $ret;

}
}
-------------------------------

This give me MySql syntax errors!

Here is the DB structure:
-------------------------------
#

# Table structure for table `obsclient_hd_priorities`

#

CREATE TABLE obsclient_hd_priorities (

PriorityID int(10) NOT NULL auto_increment,
PriorityName varchar(50) NOT NULL default '',

PRIMARY KEY (PriorityID)

) TYPE=MyISAM;
#
# Insert data into 'obsclient_hd_clients'

#
INSERT INTO obsclient_hd_priorities VALUES (1, 'Unassigned');

INSERT INTO obsclient_hd_priorities VALUES (2, 'Low');

INSERT INTO obsclient_hd_priorities VALUES (3, 'Medium');
INSERT INTO obsclient_hd_priorities VALUES (4, 'High');

INSERT INTO obsclient_hd_priorities VALUES (5, 'Critical');

# --------------------------------------------------------

#

# Table structure for table `obsclient_hd_ticketdata`

#

CREATE TABLE obsclient_hd_ticketdata (

TicketID int(10) NOT NULL auto_increment,
UID int(10) NOT NULL default '0',
StatusID int(10) NOT NULL default '0',
ClientID int(10) NOT NULL default '0',
CategoryID int(10) NOT NULL default '0',

PriorityID int(10) NOT NULL default '0',

EntryDate date NOT NULL default '0000-00-00',
TicketSubject varchar(255) NOT NULL default '',

TicketDetails text NOT NULL default '',

PRIMARY KEY (TicketID)

) TYPE=MyISAM;
#
# Insert data into 'obsclient_hd_ticketdata'

#
INSERT INTO obsclient_hd_ticketdata VALUES (1, 1, 1, 1, 1, 1, '2003-11-11', 'TestSubject1', 'TestDetail1');

INSERT INTO obsclient_hd_ticketdata VALUES (2, 1, 2, 2, 2, 2, '2003-12-12', 'TestSubject2', 'TestDetail2');

INSERT INTO obsclient_hd_ticketdata VALUES (3, 0, 3, 3, 3, 3, '2003-10-10', 'TestSubject3', 'TestDetail3');


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

Any sample code would be greatly appreciated!

JMass

2
jmass
Re: Multiple Table Joins
  • 2004/2/20 18:36

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


Thought I had it....But still not quite right.
I no longer get syntax erros but does not return the info I need.


function chooseUserTickets( $UID )
{

global $xoopsDB;
$ret = array();
$ticket_table = $xoopsDB->prefix('obsclient_hd_ticketdata');
$client_table = $xoopsDB->prefix('obsclient_clients');
$category_table = $xoopsDB->prefix('obsclient_hd_categories');
$status_table = $xoopsDB->prefix('obsclient_hd_status');
$priority_table = $xoopsDB->prefix('obsclient_hd_priorities');

$sql = "SELECT ticket.TicketID, ticket.EntryDate, ticket.TicketSubject, client.ClientName, category.CategoryName, status.StatusName, priority.PriorityName FROM $ticket_table AS ticket LEFT JOIN $client_table AS client, $category_table AS category, $status_table AS status, $priority_table AS priority ON client.ClientID = ticket.ClientID AND category.CategoryID = ticket.CategoryID AND status.StatusID = ticket.StatusID AND priority.PriorityID = ticket.PriorityID WHERE ticket.UID = $UID";

$result_choose = $xoopsDB->query($sql);
while ( $client_row = $xoopsDB->fetchArray($result_choose) )

{
$ret[] = array('TicketID' => $client_row['TicketID'], 'EntryDate' => $client_row['EntryDate'], 'TicketSubject' => $client_row['TicketSubject'], 'ClientName' => $client_row['ClientName'], 'CategoryName' => $client_row['CategoryName'], 'StatusName' => $client_row['StatusName'], 'PriorityName' => $client_row['PriorityName']);

}
return $ret;

}

Stupid "AND" instead of ","


Thanks,

JMass

3
jmass
Re: Multiple Table Joins
  • 2004/2/20 18:56

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


I think I got it this time...
I do not need a outer left join. A simple inner joint should sufice.

function chooseUserTickets( $UID )
{

global $xoopsDB;
$ret = array();
$ticket_table = $xoopsDB->prefix('obsclient_hd_ticketdata');
$client_table = $xoopsDB->prefix('obsclient_clients');
$category_table = $xoopsDB->prefix('obsclient_hd_categories');
$status_table = $xoopsDB->prefix('obsclient_hd_status');
$priority_table = $xoopsDB->prefix('obsclient_hd_priorities');

$sql = "SELECT ticket.TicketID, ticket.EntryDate, ticket.TicketSubject, client.ClientName, category.CategoryName, status.StatusName, priority.PriorityName FROM $ticket_table AS ticket JOIN $client_table AS client, $category_table AS category, $status_table AS status, $priority_table AS priority WHERE ticket.UID = $UID AND client.ClientID = ticket.ClientID AND category.CategoryID = ticket.CategoryID AND status.StatusID = ticket.StatusID AND priority.PriorityID = ticket.PriorityID";

$result_choose = $xoopsDB->query($sql);
while ( $client_row = $xoopsDB->fetchArray($result_choose) )

{
$ret[] = array('TicketID' => $client_row['TicketID'], 'EntryDate' => $client_row['EntryDate'], 'TicketSubject' => $client_row['TicketSubject'], 'ClientName' => $client_row['ClientName'], 'CategoryName' => $client_row['CategoryName'], 'StatusName' => $client_row['StatusName'], 'PriorityName' => $client_row['PriorityName']);

}
return $ret;

}
}

Seems to work!

JMass

4
Dave_L
Re: Multiple Table Joins
  • 2004/2/20 19:03

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


I don't think you can list multiple tables in a single LEFT JOIN clause like that. Reference: http://www.mysql.com/doc/en/JOIN.html

So you would need to do it this way:

SELECT ticket.TicketIDticket.EntryDateticket.TicketSubjectclient.ClientNamecategory.CategoryNamestatus.StatusNamepriority.PriorityName FROM $ticket_table AS ticket
   LEFT JOIN $client_table 
AS client ON client.ClientID ticket.ClientID
   LEFT JOIN $category_table 
AS category ON category.CategoryID ticket.CategoryID
   LEFT JOIN $status_table 
AS status ON status.StatusID ticket.StatusID
   LEFT JOIN $priority_table 
AS priority ON priority.PriorityID ticket.PriorityID
   WHERE ticket
.UID $UID

5
jmass
Re: Multiple Table Joins
  • 2004/2/20 20:18

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


I actually got the multiple Left Joins to return data (the second post's code). But the data was not filtered like I needed. The code I used (Third post's code) work perfectly. The difference is that it is an inner join not an outer one.

--------------------
An OUTER JOIN is used to return all rows that exist in one table, even though corresponding rows do not exist in the joined table.
--------------------

The Left Outer Join returned multiple instances of my tickets.

When I did an Inner Natural Join it filtered the return like I needed.

--------------------
A NATURAL JOIN is nearly the same as the EQUIJOIN; however, the NATURAL JOIN differs from the EQUIJOIN ********by eliminating duplicate columns in the joining columns.*******The JOIN condition is the same, but the columns selected differ.

The syntax is as follows:

SELECT TABLE1.*, TABLE2.COLUMN_NAME
[ TABLE3.COLUMN_NAME ]
FROM TABLE1, TABLE2 [ TABLE3 ]
WHERE TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME
[ AND TABLE1.COLUMN_NAME = TABLE3.COLUMN ]

Look at the following example:

SELECT EMPLOYEE_TBL.*, EMPLOYEE_PAY_TBL.SALARY
FROM EMPLOYEE_TBL,
EMPLOYEE_PAY_TBL
WHERE EMPLOYEE_TBL.EMP_ID = EMPLOYEE_PAY_TBL.EMP_ID;

This SQL statement returns all columns from EMPLOYEE_TBL and SALARY from the EMPLOYEE_PAY_TBL. The EMP_ID is in both tables, but is retrieved only from the EMPLOYEE_TBL because both contain the same information and do not need to be selected.

The following example selects all columns from the EMPLOYEE_TBL table and only one column from the EMPLOYEE_PAY_TBL table. Remember that the asterisk (*) represents all columns of a table.

SELECT EMPLOYEE_TBL.*, EMPLOYEE_PAY_TBL.POSITION
FROM EMPLOYEE_TBL, EMPLOYEE_PAY_TBL
WHERE EMPLOYEE_TBL.EMP_ID = EMPLOYEE_PAY_TBL.EMP_ID;

EMP_ID LAST_NAM FIRST_NA M ADDRESS CITY ST ZIP PHONE
---------- -------- -------- - ------------- ------------ -- ----- ----------
PAGER POSITION
---------- --------------
311549902 STEPHENS TINA D RR 3 BOX 17A GREENWOOD IN 47890 3178784465
MARKETING

442346889 PLEW LINDA C 3301 BEACON INDIANAPOLIS IN 46224 3172978990
TEAM LEADER

213764555 GLASS BRANDON S 1710 MAIN ST WHITELAND IN 47885 3178984321
3175709980 SALES MANAGER

313782439 GLASS JACOB 3789 RIVER BLVD INDIANAPOLIS IN 45734 3175457676
8887345678 SALESMAN

220984332 WALLACE MARIAH 7889 KEYSTONE INDIANAPOLIS IN 46741 3173325986
SHIPPER

443679012 SPURGEON TIFFANY 5 GEORGE COURT INDIANAPOLIS IN 46234 3175679007
SHIPPER


6 rows selected.


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

This info is at InformIT

JMass

Login

Who's Online

959 user(s) are online (66 user(s) are browsing Support Forums)


Members: 0


Guests: 959


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Jul 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits