Jump to content

Query Optimization


rdennisak

Recommended Posts

I have built a taxi scheduling system but I'm a bit rusty on my queries so they are a little clunky.

 

I think a JOIN query could/should be used here.

 

These queries are inside a FOR loop that iterates 7 times (a weekly calendar), which is inside a WHILE loop (displays multiple vehicles)

 

The first query gets the driver_id for a vehicle from the Schedule table

The second query gets the driver's name from the Drivers table using the driver_id from the first query.

 

$sql1 = "SELECT driver_id, sched_id FROM schedule WHERE car_id = '$car_id' and sched_date = '$weekDate';

 

$sql2 = "SELECT first_name, last_name FROM drivers WHERE driver_id = '$driver_id'";

 

What woud a JOIN query look like?

 

Here is my info:

MySQL Version 5.0.32-Debian

 

 

CREATE TABLE `schedule` (

  `sched_id` int(6) unsigned NOT NULL auto_increment,

  `sched_date` date NOT NULL,

  `sched_time` varchar(10) collate utf8_unicode_ci NOT NULL,

  `car_id` varchar(4) collate utf8_unicode_ci NOT NULL,

  `driver_id` varchar(4) collate utf8_unicode_ci NOT NULL,

    PRIMARY KEY  (`sched_id`),

  KEY `sched_date` (`sched_date`)

) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=9885 ;

 

CREATE TABLE `drivers` (

  `driver_id` int(4) unsigned NOT NULL auto_increment,

  `first_name` varchar(30) collate utf8_unicode_ci NOT NULL,

  `last_name` varchar(30) collate utf8_unicode_ci NOT NULL,

  `nickname` varchar(30) collate utf8_unicode_ci NOT NULL,

  `street` varchar(50) collate utf8_unicode_ci NOT NULL,

  `city` varchar(50) collate utf8_unicode_ci NOT NULL,

  `state` varchar(20) collate utf8_unicode_ci NOT NULL,

  `zip` varchar(11) collate utf8_unicode_ci NOT NULL,

  `phone_1` varchar(20) collate utf8_unicode_ci NOT NULL,

  `phone_2` varchar(20) collate utf8_unicode_ci NOT NULL,

  `chauffer_number` varchar(20) collate utf8_unicode_ci NOT NULL,

  `chauf_exp_date` date NOT NULL,

  `adl_number` varchar(20) collate utf8_unicode_ci NOT NULL,

  `adl_exp_date` date NOT NULL,

  `birthdate` date NOT NULL,

  `e_contact` varchar(100) collate utf8_unicode_ci NOT NULL,

  `e_phone` varchar(40) collate utf8_unicode_ci NOT NULL,

  UNIQUE KEY `driver_id` (`driver_id`)

) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=163 ;

 

Link to comment
https://forums.phpfreaks.com/topic/123973-query-optimization/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.