Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Using Subqueries In MySQL (part 1)
Build sophisticated nested queries with MySQL 4

| Turning The Tables |

Before we begin, a quick introduction to the tables I'll be using throughout this tutorial seems to be in order. In order to explain subqueries, I'll be using a fictitious company's accounting database, which consists of the following four tables:

1. The "services" table: The fictitious company under discussion offers customers a number of outsourced services, each of which is associated with a fee and has a unique service ID. This information is stored in a "services" table, which looks like this:


mysql> SELECT * FROM services;
+-----+------------------+---------+
| sid | sname            | sfee    |
+-----+------------------+---------+
|   1 | Accounting       | 1500.00 |
|   2 | Recruitment      |  500.00 |
|   3 | Data Management  |  300.00 |
|   4 | Administration   |  500.00 |
|   5 | Customer Support | 2500.00 |
|   6 | Security         |  600.00 |
+-----+------------------+---------+
6 rows in set (0.16 sec)


2. The "clients" table: The company also has a list of its current clients stored in a separate "clients" table. Each client is identified with a unique customer ID.


mysql> SELECT * FROM clients;
+-----+-----------------------------+
| cid | cname                       |
+-----+-----------------------------+
| 101 | JV Real Estate              |
| 102 | ABC Talent Agency           |
| 103 | DMW Trading                 |
| 104 | Rabbit Foods Inc            |
| 110 | Sharp Eyes Detective Agency |
+-----+-----------------------------+
5 rows in set (0.00 sec)


3. The "branches" table: Each customer may have one or more branch offices. The "branches" table lists the branch offices per customer, together with each branch's location. Each branch has a description, a unique branch ID, and a foreign key reference to the customer ID.


mysql> SELECT * FROM branches;
+------+-----+--------------------------------+------+
| bid  | cid | bdesc                          | bloc |
+------+-----+--------------------------------+------+
| 1011 | 101 | Corporate HQ                   | CA   |
| 1012 | 101 | Accounting Department          | NY   |
| 1013 | 101 | Customer Grievances Department | KA   |
| 1041 | 104 | Branch Office (East)           | MA   |
| 1042 | 104 | Branch Office (West)           | CA   |
| 1101 | 110 | Head Office                    | CA   |
| 1031 | 103 | N Region HO                    | ME   |
| 1032 | 103 | NE Region HO                   | CT   |
| 1033 | 103 | NW Region HO                   | NY   |
+------+-----+--------------------------------+------+
9 rows in set (0.01 sec)


4. The "branches_services" table: Services supplied to each branch office are listed in this table, which contains pairs of branch IDs and service IDs (foreign keys into the "branches" and "services" table respectively).


mysql> SELECT * FROM branches_services;
+------+-----+
| bid  | sid |
+------+-----+
| 1011 |   1 |
| 1011 |   2 |
| 1011 |   3 |
| 1011 |   4 |
| 1012 |   1 |
| 1013 |   5 |
| 1041 |   1 |
| 1041 |   4 |
| 1042 |   1 |
| 1042 |   4 |
| 1101 |   1 |
| 1031 |   2 |
| 1031 |   3 |
| 1031 |   4 |
| 1032 |   3 |
| 1033 |   4 |
+------+-----+
16 rows in set (0.11 sec)


The relationships between the various tables are best illustrated with the following diagram:

Output image

In order for you to try out the examples in this tutorial, you should recreate these tables in your development environment. Here are the SQL queries I used:


#
# Table structure for table `branches`
#

CREATE TABLE branches (
  bid int(8) NOT NULL default '0',
  cid tinyint(4) NOT NULL default '0',
  bdesc varchar(255) NOT NULL default '',
  bloc char(3) NOT NULL default ''
) TYPE=MyISAM CHARSET=latin1;

#
# Dumping data for table `branches`
#

INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1011, 101, 'Corporate HQ', 'CA');
INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1012, 101, 'Accounting Department', 'NY');
INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1013, 101, 'Customer Grievances Department', 'KA');
INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1041, 104, 'Branch Office (East)', 'MA');
INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1042, 104, 'Branch Office (West)', 'CA');
INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1101, 110, 'Head Office', 'CA');
INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1031, 103, 'N Region HO', 'ME');
INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1032, 103, 'NE Region HO', 'CT');
INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1033, 103, 'NW Region HO', 'NY');

#
# Table structure for table `branches_services`
#

CREATE TABLE branches_services (
  bid int(8) NOT NULL default '0',
  sid tinyint(4) NOT NULL default '0'
) TYPE=MyISAM CHARSET=latin1;

#
# Dumping data for table `branches_services`
#

INSERT INTO branches_services (bid, sid) VALUES (1011, 1);
INSERT INTO branches_services (bid, sid) VALUES (1011, 2);
INSERT INTO branches_services (bid, sid) VALUES (1011, 3);
INSERT INTO branches_services (bid, sid) VALUES (1011, 4);
INSERT INTO branches_services (bid, sid) VALUES (1012, 1);
INSERT INTO branches_services (bid, sid) VALUES (1013, 5);
INSERT INTO branches_services (bid, sid) VALUES (1041, 1);
INSERT INTO branches_services (bid, sid) VALUES (1041, 4);
INSERT INTO branches_services (bid, sid) VALUES (1042, 1);
INSERT INTO branches_services (bid, sid) VALUES (1042, 4);
INSERT INTO branches_services (bid, sid) VALUES (1101, 1);
INSERT INTO branches_services (bid, sid) VALUES (1031, 2);
INSERT INTO branches_services (bid, sid) VALUES (1031, 3);
INSERT INTO branches_services (bid, sid) VALUES (1031, 4);
INSERT INTO branches_services (bid, sid) VALUES (1032, 3);
INSERT INTO branches_services (bid, sid) VALUES (1033, 4);

#
# Table structure for table `clients`
#

CREATE TABLE clients (
  cid tinyint(4) NOT NULL default '0',
  cname varchar(255) NOT NULL default '',
  PRIMARY KEY  (cid)
) TYPE=MyISAM CHARSET=latin1;

#
# Dumping data for table `clients`
#

INSERT INTO clients (cid, cname) VALUES (101, 'JV Real Estate');
INSERT INTO clients (cid, cname) VALUES (102, 'ABC Talent Agency');
INSERT INTO clients (cid, cname) VALUES (103, 'DMW Trading');
INSERT INTO clients (cid, cname) VALUES (104, 'Rabbit Foods Inc');
INSERT INTO clients (cid, cname) VALUES (110, 'Sharp Eyes Detective Agency');

#
# Table structure for table `services`
#

CREATE TABLE services (
  sid tinyint(4) NOT NULL default '0',
  sname varchar(255) NOT NULL default '',
  sfee float(6,2) NOT NULL default '0.00',
  PRIMARY KEY  (sid)
) TYPE=MyISAM CHARSET=latin1;

#
# Dumping data for table `services`
#

INSERT INTO services (sid, sname, sfee) VALUES (1, 'Accounting', '1500.00');
INSERT INTO services (sid, sname, sfee) VALUES (2, 'Recruitment', '500.00');
INSERT INTO services (sid, sname, sfee) VALUES (3, 'Data Management', '300.00');
INSERT INTO services (sid, sname, sfee) VALUES (4, 'Administration', '500.00');
INSERT INTO services (sid, sname, sfee) VALUES (5, 'Customer Support', '2500.00');
INSERT INTO services (sid, sname, sfee) VALUES (6, 'Security', '600.00');


Once you've got your tables created, flip the page and let's start subquerying!


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback