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... - foreach ($teachers AS $row)
-
{
-
echo "<tr>";
-
$i = $i + 1;
-
echo "<td>$i.</td>
-
-
<td>" .$row[division_id]."</td>
-
<td>" .$row[nic]."</td>
-
<td>" .$row[name_initials]."</td>
-
<td>" .$row[name_full]."</td>
-
<td>". $row[date_of_birth]."</td>
-
<td>". $row[sex]."</td>
-
<td>". $row[address_permanent]."</td>
-
<td>". $row[zone_id]."</td>";
-
-
}
Can anyone please let me know how to show the data from multiple tables?
3 1325
Mufleeh,
The SQL statement you want is something similar to this... - SELECT * FROM
-
teachers t, schools s, division d, zones z
-
WHERE
-
t.schoolID = s.ID
-
AND
-
s.divisionID = d.ID
-
AND
-
d.zoneID = z.ID
-
AND
-
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... - -- --------------------------------------------------------
-
-
--
-
-- Table structure for table `zones`
-
--
-
-
CREATE TABLE IF NOT EXISTS `zones` (
-
`id` int(11) NOT NULL AUTO_INCREMENT,
-
`zone` varchar(255) NOT NULL,
-
PRIMARY KEY (`id`),
-
KEY `zone` (`zone`)
-
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
-
-
--
-
-- Dumping data for table `zones`
-
--
-
-
INSERT INTO `zones` (`id`, `zone`) VALUES(1, 'zone1');
-
INSERT INTO `zones` (`id`, `zone`) VALUES(2, 'zone2');
-
INSERT INTO `zones` (`id`, `zone`) VALUES(3, 'zone3');
-
-
-- --------------------------------------------------------
-
-
--
-
-- Table structure for table `division`
-
--
-
-
CREATE TABLE IF NOT EXISTS `division` (
-
`id` int(11) NOT NULL AUTO_INCREMENT,
-
`zoneID` int(11) NOT NULL,
-
`division` varchar(255) NOT NULL,
-
PRIMARY KEY (`id`),
-
KEY `zoneID` (`zoneID`)
-
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
-
-
--
-
-- Dumping data for table `division`
-
--
-
-
INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(1, 1, 'div1');
-
INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(2, 1, 'div2');
-
INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(3, 1, 'div3');
-
INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(4, 2, 'div4');
-
INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(5, 2, 'div5');
-
INSERT INTO `division` (`id`, `zoneID`, `division`) VALUES(6, 3, 'div6');
-
-
-- --------------------------------------------------------
-
-
--
-
-- Table structure for table `schools`
-
--
-
-
CREATE TABLE IF NOT EXISTS `schools` (
-
`id` int(11) NOT NULL AUTO_INCREMENT,
-
`divisionID` int(11) NOT NULL,
-
`school` varchar(255) NOT NULL,
-
PRIMARY KEY (`id`),
-
KEY `divisionID` (`divisionID`)
-
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
-
-
--
-
-- Dumping data for table `schools`
-
--
-
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(1, 1, 'school1');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(2, 1, 'school2');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(3, 2, 'school3');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(4, 2, 'school4');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(5, 3, 'school5');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(6, 3, 'school6');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(7, 4, 'school7');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(8, 4, 'school8');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(9, 5, 'school9');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(10, 5, 'school10');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(11, 6, 'school11');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(12, 6, 'school12');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(13, 7, 'school13');
-
INSERT INTO `schools` (`id`, `divisionID`, `school`) VALUES(14, 7, 'school14');
-
-
-- --------------------------------------------------------
-
-
--
-
-- Table structure for table `teachers`
-
--
-
-
CREATE TABLE IF NOT EXISTS `teachers` (
-
`id` int(11) NOT NULL AUTO_INCREMENT,
-
`schoolID` int(11) NOT NULL,
-
`teacher` varchar(255) NOT NULL,
-
PRIMARY KEY (`id`),
-
KEY `schoolID` (`schoolID`)
-
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=25 ;
-
-
--
-
-- Dumping data for table `teachers`
-
--
-
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(1, 1, 'teacher1');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(2, 1, 'teacher2');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(3, 2, 'teacher3');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(4, 2, 'teacher4');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(5, 3, 'teacher5');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(6, 3, 'teacher6');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(7, 4, 'teacher7');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(8, 4, 'teacher8');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(9, 5, 'teacher9');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(10, 5, 'teacher10');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(11, 6, 'teacher11');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(12, 6, 'teacher12');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(13, 7, 'teacher13');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(14, 7, 'teacher14');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(15, 8, 'teacher15');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(16, 8, 'teacher16');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(17, 9, 'teacher17');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(18, 9, 'teacher18');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(19, 10, 'teacher19');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(20, 10, 'teacher20');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(21, 11, 'teacher21');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(22, 11, 'teacher22');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(23, 12, 'teacher23');
-
INSERT INTO `teachers` (`id`, `schoolID`, `teacher`) VALUES(24, 12, 'teacher24');
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... - SELECT *
-
FROM teachers
-
WHERE schoolID IN
-
(
-
SELECT id FROM schools s WHERE s.divisionID IN
-
(
-
SELECT id FROM division d WHERE d.zoneID IN
-
(
-
SELECT id FROM zones z WHERE z.zone = 'zone1')
-
)
-
)
giving you... - $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.
Good morning John Doe,
It is really really great. Thank you very much!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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:
...
|
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
|
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...
|
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...
| |
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...
|
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
|
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...
|
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,...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |