473,473 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Showing retrieved data from multi tables

2 New Member
I am new to PHP as well as to this forum. Thanks in advance for you support.

My problem is to retrieve data from multi tables. I have tables like Zones, Division, Schools and Teachers. In a single Zone there can be many divisions and in a single division there can be many schools. I need to make a query to find out all the teachers in a particular zone.

I made the query like below,

$query = $this->dbh->prepare("SELECT * FROM teachers,school_locations where teachers.nic=(SELECT nic FROM teacher_appointments where teacher_appointments.census_id =(SELECT census_id FROM school_locations where school_locations.division_id=(SELECT division_id FROM divisions WHERE divisions.division_name='$_POST[division_name]')))");

this works fine when there is only one zone, one division and one school. I used foreach loop to view the data on this case and the problem is when there are many schools for a given division.

My foreach loop looks like below...


Expand|Select|Wrap|Line Numbers
  1. foreach ($teachers AS $row)
  2.                         {        
  3.                             echo "<tr>";
  4.                             $i = $i + 1;
  5.                             echo "<td>$i.</td>
  6.  
  7.                         <td>" .$row[division_id]."</td>    
  8.                         <td>" .$row[nic]."</td>
  9.                         <td>" .$row[name_initials]."</td>
  10.                         <td>" .$row[name_full]."</td>
  11.                         <td>".  $row[date_of_birth]."</td>
  12.                         <td>".  $row[sex]."</td>
  13.                         <td>".  $row[address_permanent]."</td>
  14.                         <td>".  $row[zone_id]."</td>";
  15.  
  16. }

Can anyone please let me know how to show the data from multiple tables?
Jul 4 '11 #1
3 1325
John Doe
20 New Member
Mufleeh,

The SQL statement you want is something similar to this...

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM 
  2. teachers t, schools s, division d, zones z
  3. WHERE 
  4. t.schoolID = s.ID
  5. AND
  6. s.divisionID = d.ID
  7. AND
  8. d.zoneID = z.ID
  9. AND
  10. z.name = 'zone1'

If you use this format, you'll need to change the '*' to a list of the fields that you want returned, otherwise you'll get all fields from all tables!
eg. SELECT t.nic, t.name_initials, t.name_full, t.sex FROM ... etc.


The above SQL is based on the following test table structures, but I think you've already got the idea of linking ID fields between tables...


Expand|Select|Wrap|Line Numbers
  1. -- --------------------------------------------------------
  2.  
  3. --
  4. -- Table structure for table `zones`
  5. --
  6.  
  7. CREATE TABLE IF NOT EXISTS `zones` (
  8.   `id` int(11) NOT NULL AUTO_INCREMENT,
  9.   `zone` varchar(255) NOT NULL,
  10.   PRIMARY KEY (`id`),
  11.   KEY `zone` (`zone`)
  12. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
  13.  
  14. --
  15. -- Dumping data for table `zones`
  16. --
  17.  
  18. INSERT INTO `zones` (`id`, `zone`) VALUES(1, 'zone1');
  19. INSERT INTO `zones` (`id`, `zone`) VALUES(2, 'zone2');
  20. INSERT INTO `zones` (`id`, `zone`) VALUES(3, 'zone3');
  21.  
  22. -- --------------------------------------------------------
  23.  
  24. --
  25. -- Table structure for table `division`
  26. --
  27.  
  28. CREATE TABLE IF NOT EXISTS `division` (
  29.   `id` int(11) NOT NULL AUTO_INCREMENT,
  30.   `zoneID` int(11) NOT NULL,
  31.   `division` varchar(255) NOT NULL,
  32.   PRIMARY KEY (`id`),
  33.   KEY `zoneID` (`zoneID`)
  34. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
  35.  
  36. --
  37. -- Dumping data for table `division`
  38. --
  39.  
  40. INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(1, 1, 'div1');
  41. INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(2, 1, 'div2');
  42. INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(3, 1, 'div3');
  43. INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(4, 2, 'div4');
  44. INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(5, 2, 'div5');
  45. INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(6, 3, 'div6');
  46.  
  47. -- --------------------------------------------------------
  48.  
  49. --
  50. -- Table structure for table `schools`
  51. --
  52.  
  53. CREATE TABLE IF NOT EXISTS `schools` (
  54.   `id` int(11) NOT NULL AUTO_INCREMENT,
  55.   `divisionID` int(11) NOT NULL,
  56.   `school` varchar(255) NOT NULL,
  57.   PRIMARY KEY (`id`),
  58.   KEY `divisionID` (`divisionID`)
  59. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
  60.  
  61. --
  62. -- Dumping data for table `schools`
  63. --
  64.  
  65. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(1, 1, 'school1');
  66. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(2, 1, 'school2');
  67. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(3, 2, 'school3');
  68. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(4, 2, 'school4');
  69. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(5, 3, 'school5');
  70. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(6, 3, 'school6');
  71. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(7, 4, 'school7');
  72. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(8, 4, 'school8');
  73. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(9, 5, 'school9');
  74. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(10, 5, 'school10');
  75. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(11, 6, 'school11');
  76. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(12, 6, 'school12');
  77. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(13, 7, 'school13');
  78. INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(14, 7, 'school14');
  79.  
  80. -- --------------------------------------------------------
  81.  
  82. --
  83. -- Table structure for table `teachers`
  84. --
  85.  
  86. CREATE TABLE IF NOT EXISTS `teachers` (
  87.   `id` int(11) NOT NULL AUTO_INCREMENT,
  88.   `schoolID` int(11) NOT NULL,
  89.   `teacher` varchar(255) NOT NULL,
  90.   PRIMARY KEY (`id`),
  91.   KEY `schoolID` (`schoolID`)
  92. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=25 ;
  93.  
  94. --
  95. -- Dumping data for table `teachers`
  96. --
  97.  
  98. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(1, 1, 'teacher1');
  99. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(2, 1, 'teacher2');
  100. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(3, 2, 'teacher3');
  101. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(4, 2, 'teacher4');
  102. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(5, 3, 'teacher5');
  103. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(6, 3, 'teacher6');
  104. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(7, 4, 'teacher7');
  105. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(8, 4, 'teacher8');
  106. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(9, 5, 'teacher9');
  107. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(10, 5, 'teacher10');
  108. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(11, 6, 'teacher11');
  109. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(12, 6, 'teacher12');
  110. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(13, 7, 'teacher13');
  111. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(14, 7, 'teacher14');
  112. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(15, 8, 'teacher15');
  113. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(16, 8, 'teacher16');
  114. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(17, 9, 'teacher17');
  115. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(18, 9, 'teacher18');
  116. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(19, 10, 'teacher19');
  117. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(20, 10, 'teacher20');
  118. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(21, 11, 'teacher21');
  119. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(22, 11, 'teacher22');
  120. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(23, 12, 'teacher23');
  121. INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(24, 12, 'teacher24');
Jul 4 '11 #2
John Doe
20 New Member
Actually, not knowing the data structures of your tables, you may be able to achieve what you want just by changing the '=' comparison to an 'IN' comparison, like so...


Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM teachers
  3. WHERE schoolID IN  
  4. (
  5. SELECT id FROM schools s WHERE s.divisionID IN
  6. (
  7. SELECT id FROM division d WHERE d.zoneID IN 
  8. (
  9. SELECT id FROM zones z WHERE z.zone =  'zone1')
  10. )
  11. )

giving you...

Expand|Select|Wrap|Line Numbers
  1. $query = $this->dbh->prepare("SELECT * FROM teachers,school_locations where teachers.nic IN (SELECT nic FROM teacher_appointments where teacher_appointments.census_id IN (SELECT census_id FROM school_locations where school_locations.division_id IN (SELECT division_id FROM divisions WHERE divisions.division_name='$_POST[division_name]')))");
The difference is that IN allows many results to be returned, whereas '=' is expecting only one result to compare against.
Jul 4 '11 #3
Mufleeh
2 New Member
Good morning John Doe,

It is really really great. Thank you very much!
Jul 5 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Marek Kotowski | last post by:
In MySQL online documentation there are some examples with multi-tables left joins. But all of them are like this (taken from the documentation): SELECT ... FROM table1 LEFT JOIN table2 on...
6
by: J Belly | last post by:
Hi, all: This is probably a simple problem, but, as an SQL newbie, I'm having a little trouble understanding multi-joins and subqueries. I have the following tables and columns: ...
1
by: Sylvain Audet | last post by:
Hi, Does XMLReader supports multi tables returned from a SQL Server Stored Procedure? Ex: Begin Select 1 .. FOR XML AUTO
2
by: narasingarao | last post by:
Hi to group, I'm a student of M.C.A. from B.I.T. Ranchi...I'm in my project period here i have to migrate the MS-Access database table to Oracle data base tables...so, please help me in getting...
2
by: swoozie | last post by:
Hello, I'm trying to display two sets of tabluar data in two tables. There is a submit button on the page to launch everything, and a refresh button to update the tables without having a...
0
by: Mikaël PLOUHINEC | last post by:
Hello, I would like to know something. I have a SQL database. In my database, I have two tables : Client and Type_Client For example, the fields of my Client table is ID, Name, type_client_Id...
11
by: khushbubhalla | last post by:
how to compare data between tables and views row by row , column by column in the test and production environment
1
by: chanlichin | last post by:
Can any one help me, thanks.My problem is retrieved data from database but when i run the coding, the combo box do not display anything.The below is my coding: Dim conn As New...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.