I have the following query:
SELECT t.TOUR, t.ROUND, t.ID1, t.ID2
FROM belgarath.today_atp AS t
LEFT OUTER JOIN belgarath.match_ as m
ON m.tour_id = 0
AND t.TOUR = m.tours_ID_T
AND t.ROUND = m.rounds_ID_R
AND t.ID1 = m.today_players_ID1
AND t.ID2 = m.today_players_ID2
WHERE m.id_ IS NULL;
I'm looking to get the records from today_atp where they don't exist in match_.
I've given up on the query after 30 mins as this seems like it's taking too long for tables which have 700 rows (today_atp) and 1.4m rows (match_).
I tried just pulling one record via this query:
SELECT t.TOUR, t.ROUND, t.ID1, t.ID2
FROM belgarath.today_atp AS t
LEFT OUTER JOIN belgarath.match_ as m
ON m.tour_id = 0
AND t.TOUR = m.tours_ID_T
AND t.ROUND = m.rounds_ID_R
AND t.ID1 = m.today_players_ID1
AND t.ID2 = m.today_players_ID2
WHERE m.id_ IS NULL
AND t.TOUR = 16756
AND t.ROUND = 2
AND t.ID1 = 29591
AND t.ID2 = 37741;
This takes 30 seconds.
The specs for the two tables are as follows:
CREATE TABLE `match_` (
`id_` int DEFAULT NULL,
`date_time_scheduled` datetime DEFAULT NULL,
`date_time_actual` datetime DEFAULT NULL,
`tour_id` int NOT NULL,
`tournament_id` int DEFAULT NULL,
`tours_ID_T` int NOT NULL,
`rounds_ID_R` int NOT NULL,
`today_players_ID1` int DEFAULT NULL,
`today_players_ID2` int DEFAULT NULL,
`games_players_ID1` int DEFAULT NULL,
`games_players_ID2` int DEFAULT NULL,
`uncertainty_bin` int DEFAULT NULL,
`p1_win_pred_ogion` float DEFAULT NULL,
`p1_win_pred_rf_ogion` float DEFAULT NULL,
`p1_win_pred_ged` float DEFAULT NULL,
`p1_win_pred_rf_ged` float DEFAULT NULL,
`url` varchar(255) DEFAULT NULL,
`winning_player` int DEFAULT NULL,
`completed_sets` int DEFAULT NULL,
`result_type_id` int DEFAULT NULL,
`p1_pinnacle_closing_odds` float DEFAULT NULL,
`p2_pinnacle_closing_odds` float DEFAULT NULL,
`p1_pinnacle_opening_odds` float DEFAULT NULL,
`p2_pinnacle_opening_odds` float DEFAULT NULL,
`post_match_data_retrieved` int DEFAULT NULL,
`updated` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`created` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
KEY `ix_belgarath_match__tours_ID_T` (`tours_ID_T`),
KEY `ix_belgarath_match__today_players_ID1` (`today_players_ID1`),
KEY `ix_belgarath_match__games_players_ID1` (`games_players_ID1`),
KEY `ix_belgarath_match__tour_id` (`tour_id`),
KEY `ix_belgarath_match__rounds_ID_R` (`rounds_ID_R`),
KEY `ix_belgarath_match__today_players_ID2` (`today_players_ID2`),
KEY `ix_belgarath_match__games_players_ID2` (`games_players_ID2`),
KEY `ix_belgarath_match__uncertainty_bin` (`uncertainty_bin`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
And:
CREATE TABLE `today_atp` (
`TOUR` int NOT NULL DEFAULT '0',
`DATE_GAME` datetime DEFAULT NULL,
`ID1` int NOT NULL DEFAULT '0',
`ID2` int NOT NULL DEFAULT '0',
`ROUND` int NOT NULL DEFAULT '0',
`DRAW` int NOT NULL DEFAULT '0',
`RESULT` tinytext,
`COMPLETE` tinyint unsigned DEFAULT '0',
`live` char(70) DEFAULT NULL,
`TIME_GAME` datetime DEFAULT NULL,
`RESERVE_INT` smallint DEFAULT NULL,
`RESERVE_CHAR` char(64) DEFAULT NULL,
KEY `today_atp_TOUR` (`TOUR`) /*!80000 INVISIBLE */,
KEY `today_atp_ROUND` (`ROUND`) /*!80000 INVISIBLE */,
KEY `today_atp_ID1` (`ID1`) /*!80000 INVISIBLE */,
KEY `today_atp_ID2` (`ID2`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Is there anything obvious that jumps out as to why it would be taking so long?
I have seen from other posts that CHARSET can be a problem but changing today_atp to utf8mb4 doesn't make a difference (I'm not sure how to set the COLLATE part via workbench).
(P.S. Some normalisation of the tables is definitely on my to do list!)
Related
I know this question is not new, but it has never been answered for an UPDATE statement.
I have the following error:
update `Employees` set `pronoun` = 'Frau', `first_name` = 'Sarah', `last_name` = 'Sallison', `pk_number` = 'HDSU283', `cost_center_id` = 2, `roles_id` = NULL, `start_date` = '2016-11-01', `switch` = 0, `end_date` = NULL, `office_id` = 2, `d_number` = NULL, `vk` = NULL, `employment_type_id` = 1, `service_provider_id` = NULL where `id` = 767
ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value count at row 1
With the following table:
CREATE TABLE `Employees` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`pronoun` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`first_name` text,
`last_name` text,
`employment_type_id` int unsigned NOT NULL DEFAULT '1',
`service_provider_id` int unsigned DEFAULT NULL,
`pk_number` text,
`office_id` int unsigned DEFAULT NULL,
`cost_center_id` int unsigned DEFAULT NULL,
`switch` tinyint(1) NOT NULL DEFAULT '0',
`e_mail` text,
`phone` text,
`start_date` date DEFAULT NULL,
`end_date` date DEFAULT NULL,
`profile_updated_date` date DEFAULT NULL,
`mentor_id` int unsigned DEFAULT NULL,
`vk` decimal(10,0) DEFAULT NULL,
`roles_id` int unsigned DEFAULT NULL,
`d_number` text,
`opt_mentoring` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1176 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
I don't even understand how this error is possible, if not a column doesn't exist, on an UPDATE STATEMENT.
I'm having an issue with a particular SQL query running on one of my databases:
select
`map`,
`tier`,
(select count(*) from mapzones a where a.map = b.map
and `track` = 0 and `type` > 0) as `stages`,
(select count(*) from mapzones a where a.map = b.map
and `track` > 0 and `type` > 0) as `bonuses`
from
maptiers b
order by `map` asc;
The response takes ~28-30 seconds to execute, which is much too slow for what I need.
Strangely, when I execute a similar query on another database for a separate gamemode, it takes <100ms, usually <50ms:
select
`mapname`,
`tier`,
(select count(*) from ck_zones a where a.mapname = b.mapname
and `zonegroup` = 0 and (zonetype = 2
or zonetype = 3)) as `stages`,
(select count(*) from ck_zones a where a.mapname = b.mapname
and `zonegroup` > 0 and `zonetype` = 0) as `bonuses`
from
ck_maptier b
order by `mapname` asc;
Server Info
Server version: 8.0.29 - MySQL Community Server - GPL
Collation/Type
First Query's Database (BhopTimer)
Second Query's Database (SurfTimer)
(from Comment)
SELECT a.map, a.tier, b.stage_count
FROM maptiers a
INNER JOIN
(
SELECT DISTINCT map,
COUNT(CASE WHEN (type > 0 AND track = 0) THEN 1 END)
OVER (PARTITION BY map) AS `stage_count`
FROM mapzones
) AS b ON a.map = b.map
ORDER BY a.map;
Create Tables
CREATE TABLE `maptiers` (
`map` varchar(255) NOT NULL,
`tier` int NOT NULL DEFAULT '1',
PRIMARY KEY (`map`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE `mapzones` (
`id` int NOT NULL AUTO_INCREMENT,
`map` varchar(255) NOT NULL,
`type` int DEFAULT NULL,
`corner1_x` float DEFAULT NULL,
`corner1_y` float DEFAULT NULL,
`corner1_z` float DEFAULT NULL,
`corner2_x` float DEFAULT NULL,
`corner2_y` float DEFAULT NULL,
`corner2_z` float DEFAULT NULL,
`destination_x` float NOT NULL DEFAULT '0',
`destination_y` float NOT NULL DEFAULT '0',
`destination_z` float NOT NULL DEFAULT '0',
`track` int NOT NULL DEFAULT '0',
`flags` int DEFAULT '0',
`data` int DEFAULT '0',
`form` tinyint DEFAULT NULL,
`target` varchar(63) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14612 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE `ck_maptier` (
`mapname` varchar(54) NOT NULL,
`tier` int NOT NULL,
`maxvelocity` float NOT NULL DEFAULT '3500',
`announcerecord` int NOT NULL DEFAULT '0',
`gravityfix` int NOT NULL DEFAULT '1',
`ranked` int NOT NULL DEFAULT '1',
`stages` int DEFAULT NULL,
`bonuses` int DEFAULT NULL,
PRIMARY KEY (`mapname`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE `ck_zones` (
`mapname` varchar(54) NOT NULL,
`zoneid` int NOT NULL DEFAULT '-1',
`zonetype` int DEFAULT '-1',
`zonetypeid` int DEFAULT '-1',
`pointa_x` float DEFAULT '-1',
`pointa_y` float DEFAULT '-1',
`pointa_z` float DEFAULT '-1',
`pointb_x` float DEFAULT '-1',
`pointb_y` float DEFAULT '-1',
`pointb_z` float DEFAULT '-1',
`vis` int DEFAULT '0',
`team` int DEFAULT '0',
`zonegroup` int NOT NULL DEFAULT '0',
`zonename` varchar(128) DEFAULT NULL,
`hookname` varchar(128) DEFAULT 'None',
`targetname` varchar(128) DEFAULT 'player',
`onejumplimit` int NOT NULL DEFAULT '1',
`prespeed` int NOT NULL DEFAULT '350',
PRIMARY KEY (`mapname`,`zoneid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
Samples
First Query (BhopTimer)
maptiers.sql
mapzones.sql
Second Query (SurfTimer)
ck_maptier.sql
ck_zones.sql
See if they run faster when turned inside out:
SELECT b.map,
b.tier,
x.stages,
x.bonuses
FROM ( SELECT map,
SUM(track = 0 AND type = 0) AS stages,
SUM(track > 0 AND type > 0) AS bonuses
FROM mapzones
GROUP BY map ) AS x
JOIN maptiers AS b ON b.map = x.map
ORDER BY map ASC;
INDEXes:
mapzones: INDEX(map, track, type)
maptiers: INDEX(map, tier)
(For further discussion, please provide SHOW CREATE TABLE for each table. Also: EXPLAIN SELECT ...)
I have the following query:
SELECT count(m.p1_elo_one)
FROM belgarath.match_result as m
INNER JOIN belgarath.tournament AS tm ON tm.id_ = m.tournament_id
INNER JOIN belgarath.tour AS tr ON tr.id_ = tm.tour_id
INNER JOIN belgarath.rank AS rk ON rk.id_ = tm.rank_id
INNER JOIN belgarath.round AS rd ON rd.id_ = m.round_id
INNER JOIN belgarath.parameters_match AS pm
ON pm.tour_id = tm.tour_id
AND pm.rank_id = tm.rank_id
AND pm.round_id = m.round_id
INNER JOIN belgarath.surf ON belgarath.surf.id_ = tm.surf
INNER JOIN belgarath.player AS p ON p.id_ = m.p1_id
WHERE
belgarath.surf.surf_std_id = 1
AND pm.engineer = 1
AND m.date_time_inferred < "2003-06-01"
AND m.date_time_inferred > "2000-06-01"
AND m.p1_elo_one IS NOT NULL
AND p.name_ NOT LIKE "%/%"
The match_result table is about 1.4m records and the query runs fine in about 0.4 seconds and returns a count of circa 20k. Great.
However, when I change belgarath.surf.surf_std_id = 1 to belgarath.surf.surf_std_id = 2 then the query suddenly takes around 35 seconds to return a count of circa 20k.
This has me stumped as I'm only changing an input variable, nothing else.
Any ideas on where the issue might be?
Edit:
SHOW CREATE TABLE for match_result:
'CREATE TABLE `match_result` (
`id_` int NOT NULL AUTO_INCREMENT,
`date_time_op_scheduled` datetime DEFAULT NULL,
`date_time_op_actual` datetime DEFAULT NULL,
`date_time_oc_actual` datetime DEFAULT NULL,
`date_time_inferred` datetime DEFAULT NULL,
`tournament_id` int DEFAULT NULL,
`round_id` tinyint DEFAULT NULL,
`p1_id` int DEFAULT NULL,
`p2_id` int DEFAULT NULL,
`result` varchar(45) DEFAULT NULL,
`uncertainty` float DEFAULT NULL,
`uncertainty_bin` int DEFAULT NULL,
`p1_win_pred` float DEFAULT NULL,
`p1_win_SD` float DEFAULT NULL,
`url` varchar(255) DEFAULT NULL,
`winning_player` int DEFAULT NULL,
`completed_sets` int DEFAULT NULL,
`result_type_id` int DEFAULT NULL,
`p1_pinnacle_closing_odds` float DEFAULT NULL,
`p2_pinnacle_closing_odds` float DEFAULT NULL,
`post_match_data_retrieved` int DEFAULT NULL,
`p1_elo_all` float DEFAULT NULL,
`p1_elo_exc_ret_all` float DEFAULT NULL,
`p1_h2h_win_one_time` float DEFAULT NULL,
`p1_h2h_win_one_none` int DEFAULT NULL,
`p1_h2h_win_all_time_surf` float DEFAULT NULL,
`p1_h2h_win_all_surf` float DEFAULT NULL,
`p1_h2h_win_all_time` float DEFAULT NULL,
`p1_h2h_win_all_none` int DEFAULT NULL,
`p1_h2h_one_time` float DEFAULT NULL,
`p1_h2h_one_none` int DEFAULT NULL,
`p1_h2h_all_time_surf` float DEFAULT NULL,
`p1_h2h_all_surf` float DEFAULT NULL,
`p1_h2h_all_time` float DEFAULT NULL,
`p1_h2h_all_none` int DEFAULT NULL,
`p1_win_one_time` float DEFAULT NULL,
`p1_win_one_none` int DEFAULT NULL,
`p1_win_all_time_surf` float DEFAULT NULL,
`p1_win_all_surf` float DEFAULT NULL,
`p1_win_all_time` float DEFAULT NULL,
`p1_win_all_none` int DEFAULT NULL,
`p1_match_one_time` float DEFAULT NULL,
`p1_match_one_none` int DEFAULT NULL,
`p1_match_all_time_surf` float DEFAULT NULL,
`p1_match_all_surf` float DEFAULT NULL,
`p1_match_all_time` float DEFAULT NULL,
`p1_match_all_none` int DEFAULT NULL,
`p1_elo_one` float DEFAULT NULL,
`p1_elo_exc_ret_one` float DEFAULT NULL,
`p1_jet_lag` float DEFAULT NULL,
`p1_fatigue_short_term` int DEFAULT NULL,
`p1_fatigue_long_term` int DEFAULT NULL,
`p1_retired_last_match` tinyint DEFAULT NULL,
`p2_elo_all` float DEFAULT NULL,
`p2_elo_exc_ret_all` float DEFAULT NULL,
`p2_h2h_win_one_time` float DEFAULT NULL,
`p2_h2h_win_one_none` int DEFAULT NULL,
`p2_h2h_win_all_time_surf` float DEFAULT NULL,
`p2_h2h_win_all_surf` float DEFAULT NULL,
`p2_h2h_win_all_time` float DEFAULT NULL,
`p2_h2h_win_all_none` int DEFAULT NULL,
`p2_h2h_one_time` float DEFAULT NULL,
`p2_h2h_one_none` int DEFAULT NULL,
`p2_h2h_all_time_surf` float DEFAULT NULL,
`p2_h2h_all_surf` float DEFAULT NULL,
`p2_h2h_all_time` float DEFAULT NULL,
`p2_h2h_all_none` int DEFAULT NULL,
`p2_win_one_time` float DEFAULT NULL,
`p2_win_one_none` int DEFAULT NULL,
`p2_win_all_time_surf` float DEFAULT NULL,
`p2_win_all_surf` float DEFAULT NULL,
`p2_win_all_time` float DEFAULT NULL,
`p2_win_all_none` int DEFAULT NULL,
`p2_match_one_time` float DEFAULT NULL,
`p2_match_one_none` int DEFAULT NULL,
`p2_match_all_time_surf` float DEFAULT NULL,
`p2_match_all_surf` float DEFAULT NULL,
`p2_match_all_time` float DEFAULT NULL,
`p2_match_all_none` int DEFAULT NULL,
`p2_elo_one` float DEFAULT NULL,
`p2_elo_exc_ret_one` float DEFAULT NULL,
`p2_jet_lag` float DEFAULT NULL,
`p2_fatigue_short_term` int DEFAULT NULL,
`p2_fatigue_long_term` int DEFAULT NULL,
`p2_retired_last_match` tinyint DEFAULT NULL,
`engineered` tinyint DEFAULT NULL,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_`),
KEY `ix_belgarath_match__completed_sets` (`completed_sets`),
KEY `ix_belgarath_match__post_match_data_retrieved` (`post_match_data_retrieved`),
KEY `ix_belgarath_match__date_time_oc_actual` (`date_time_oc_actual`),
KEY `ix_belgarath_match__tournament_id` (`tournament_id`),
KEY `ix_belgarath_match__round_id` (`round_id`),
KEY `ix_belgarath_match__player_id_2` (`p2_id`),
KEY `ix_belgarath_match__winning_player` (`winning_player`),
KEY `ix_belgarath_match__result_type_id` (`result_type_id`),
KEY `ix_belgarath_match__uncertainty_bin` (`uncertainty_bin`),
KEY `ix_belgarath_match__date_time_inferred` (`date_time_inferred`),
KEY `ix_belgarath_match__date_time_op_actual` (`date_time_op_actual`),
KEY `ix_belgarath_match__player_id_1` (`p1_id`),
KEY `ix_belgarath_match__date_time_op_scheduled` (`date_time_op_scheduled`) /*!80000 INVISIBLE */,
KEY `ix_belgarath_match__engineered` (`engineered`),
CONSTRAINT `match_result__player_1` FOREIGN KEY (`p1_id`) REFERENCES `player` (`id_`),
CONSTRAINT `match_result__player_2` FOREIGN KEY (`p2_id`) REFERENCES `player` (`id_`),
CONSTRAINT `match_result__round` FOREIGN KEY (`round_id`) REFERENCES `round` (`id_`),
CONSTRAINT `match_result__tournament` FOREIGN KEY (`tournament_id`) REFERENCES `tournament` (`id_`)
) ENGINE=InnoDB AUTO_INCREMENT=1408022 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci'
EXPLAIN for the query:
When running the following query using GROUP BY it takes way too much time.
SELECT specialities.id AS ID_DEPARTMENT,
specialities.name AS ID_DEPARTMENT_NAME,
agenda.idagenda AS ID_SERVICE,
agenda.name AS ID_SERVICE_NAME,
supervisor.clients_waiting AS CWaiting,
IFNULL(supervisor.clients_resent_waiting_area, 0) AS CWaiting_Resent_Area,
supervisor.clients_attending AS CAttending,
supervisor.clients_attended AS CAttended,
(SELECT SUM(TIME_TO_SEC(TIMEDIFF(NOW(), time_waiting)) / CWaiting)
FROM supervisor_time_data
WHERE supervisor_time_data.id_service = supervisor.id_service) AS TME,
(SELECT SUM(TIME_TO_SEC(TIMEDIFF(NOW(), time_attending)) / CAttending)
FROM supervisor_time_data
WHERE supervisor_time_data.id_service = supervisor.id_service) AS TMA,
(SELECT TIME_TO_SEC(MAX(TIMEDIFF(NOW(), time_waiting)))
FROM supervisor_time_data
WHERE supervisor_time_data.id_service = supervisor.id_service) AS MTE,
(SELECT TIME_TO_SEC(MAX(TIMEDIFF(NOW(), time_attending)))
FROM supervisor_time_data
WHERE supervisor_time_data.id_service = supervisor.id_service) AS MTA,
supervisor.tme_accumulated AS TME_ACCUMULATED,
supervisor.tma_accumulated AS TMA_ACCUMULATED
FROM supervisor, supervisor_time_data, agenda, specialities
WHERE supervisor.id_service = agenda.id
AND supervisor_time_data.id_service = supervisor.id_service
AND agenda.idspeciality = specialities.id
AND supervisor.booked_or_sequential = 0
AND supervisor.id_service IN (1,2,3)
GROUP BY supervisor.id_service
ORDER BY agenda.name ASC;
It takes over 3 seconds, when commenting the GROUP BY line it takes 8 ms.
Any ideas on how I can optimise this query?
Thanks
EDIT:
CREATE TABLE `supervisor` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`id_department` int(6) DEFAULT NULL,
`id_service` int(9) DEFAULT NULL,
`clients_waiting` int(6) DEFAULT '0',
`clients_attending` int(6) DEFAULT '0',
`clients_attended` int(6) DEFAULT '0',
`tma_accumulated` int(9) DEFAULT '0',
`tme_accumulated` int(9) DEFAULT '0',
`clients_resent_waiting_area` int(6) DEFAULT NULL,
`booked_or_sequential` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id_service` (`id_service`),
KEY `booked_or_sequential` (`booked_or_sequential`),
KEY `clients_waiting` (`clients_waiting`)
) ENGINE=MyISAM AUTO_INCREMENT=172 DEFAULT CHARSET=latin1;
.
CREATE TABLE `supervisor_time_data` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`id_ogs` int(32) DEFAULT NULL,
`booked_or_sequential` tinyint(1) DEFAULT NULL,
`time_waiting` datetime DEFAULT NULL,
`time_attending` datetime DEFAULT NULL,
`status` int(2) DEFAULT '0',
`id_service` int(6) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id_service` (`id_service`),
KEY `time_waiting` (`time_waiting`),
KEY `time_attending` (`time_attending`),
KEY `booked_or_sequential` (`booked_or_sequential`)
) ENGINE=MyISAM AUTO_INCREMENT=2281 DEFAULT CHARSET=latin1;
.
CREATE TABLE `agenda` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`idagenda` varchar(255) DEFAULT NULL,
`iduser` int(3) DEFAULT NULL,
`date_created` datetime DEFAULT NULL,
`agendatype` tinyint(4) DEFAULT NULL,
`idspeciality` int(6) DEFAULT NULL,
`denomination` varchar(255) DEFAULT NULL,
`ticket_count` int(3) DEFAULT NULL,
`waiting` int(3) DEFAULT NULL,
`ticket_start` int(3) DEFAULT NULL,
`ticket_end` int(3) DEFAULT NULL,
`ticket_letter` varchar(12) DEFAULT NULL,
`idcenter` int(9) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idagenda` (`idagenda`),
KEY `idspeciality` (`idspeciality`)
) ENGINE=MyISAM AUTO_INCREMENT=2228 DEFAULT CHARSET=latin1;
.
CREATE TABLE `specialities` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`date_created` datetime DEFAULT NULL,
`idwaitingarea` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=149 DEFAULT CHARSET=latin1;
Here is your queries with joins. Post your schema if this does not work.
SELECT
specialities.id AS ID_DEPARTMENT,
specialities.name AS ID_DEPARTMENT_NAME,
agenda.idagenda AS ID_SERVICE,
agenda.name AS ID_SERVICE_NAME,
supervisor.clients_waiting AS CWaiting,
IFNULL(supervisor.clients_resent_waiting_area, 0) AS CWaiting_Resent_Area,
supervisor.clients_attending AS CAttending,
supervisor.clients_attended AS CAttended,
SUM(TIME_TO_SEC(TIMEDIFF(NOW(), supervisor_time_data.time_waiting)) / supervisor.clients_waiting) AS TME,
SUM(TIME_TO_SEC(TIMEDIFF(NOW(), supervisor_time_data.time_attending)) / supervisor.clients_attending) AS TMA,
TIME_TO_SEC(MAX(TIMEDIFF(NOW(), supervisor_time_data.time_waiting))) AS MTE,
TIME_TO_SEC(MAX(TIMEDIFF(NOW(), supervisor_time_data.time_attending))) AS MTA,
supervisor.tme_accumulated AS TME_ACCUMULATED,
supervisor.tma_accumulated AS TMA_ACCUMULATED
FROM supervisor
LEFT JOIN supervisor_time_data
ON supervisor_time_data.id_service = supervisor.id_service
LEFT JOIN agenda
ON supervisor.id_service = agenda.id
LEFT JOIN specialities
ON agenda.idspeciality = specialities.id
WHERE supervisor.booked_or_sequential = 0
AND supervisor.id_service IN(1,2,3)
GROUP BY supervisor.id_service
ORDER BY agenda.name ASC;
I have the following SQL statement:
SELECT user_accounts.uacc_id,
user_accounts.uacc_username,
ride_rides.ride_type,
ride_rides.ride_num_seats,
ride_rides.ride_price_seat,
ride_rides.ride_accept_nm,
ride_rides.ride_split_cost,
ride_rides.ride_from,
ride_rides.ride_from_lat,
ride_rides.ride_from_lng,
ride_rides.ride_to,
ride_rides.ride_to_lat,
ride_rides.ride_to_lng,
user_profiles.upro_image_name,
ride_times.ridetms_id,
ride_times.ridetms_return,
ride_times.ridetms_depart_date,
ride_times.ridetms_depart_time,
ride_times.ridetms_return_date,
ride_times.ridetms_return_time,
depart_times.dpttme_text
FROM ride_times
LEFT JOIN ride_rides
ON ride_rides.ride_id = ride_times.ridetms_ride_fk
LEFT JOIN user_accounts
ON ride_rides.ride_uacc_fk = user_accounts.uacc_id
LEFT JOIN user_profiles
ON user_profiles.upro_uacc_fk = user_accounts.uacc_id
LEFT JOIN depart_times
ON depart_times.dpttme_id = ride_times.ridetms_depart_time
WHERE ride_times.ridetms_id = ?"
Right now, I have the query pulling a text representation of the data from ride_times.ridetms_depart_time in the last join, and it works fine. However, I need to do the same with another column in the ride_times table. I think I need to use an alias, but after reading several sources on aliases, I can't wrap my head around how to change the call.
Also, 100 brownie points for any feedback about any glaring mistakes in this call. It is my first attempt at using JOINs.
take care,
lee
Thanks to the responses I've received so far. Here is the structure of the tables involved:
CREATE TABLE `depart_times` (
`dpttme_id` int(11) NOT NULL,
`dpttme_text` varchar(50) DEFAULT NULL,
PRIMARY KEY (`dpttme_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `ride_rides` (
`ride_id` int(11) NOT NULL AUTO_INCREMENT,
`ride_uacc_fk` int(11) NOT NULL,
`ride_date_added` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`ride_type` tinyint(4) DEFAULT NULL,
`ride_from` varchar(200) DEFAULT NULL,
`ride_from_lat` float(10,6) DEFAULT NULL,
`ride_from_lng` float(10,6) DEFAULT NULL,
`ride_to` varchar(200) DEFAULT NULL,
`ride_to_lat` float(10,6) DEFAULT NULL,
`ride_to_lng` float(10,6) DEFAULT NULL,
`ride_num_seats` tinyint(4) DEFAULT NULL,
`ride_price_seat` float DEFAULT NULL,
`ride_accept_nm` tinyint(1) DEFAULT '0' COMMENT 'accept non-monetary items',
`ride_split_cost` tinyint(1) DEFAULT '0',
`ride_notes` longtext,
PRIMARY KEY (`ride_id`)
) ENGINE=MyISAM AUTO_INCREMENT=34 DEFAULT CHARSET=latin1;
CREATE TABLE `ride_times` (
`ridetms_id` int(11) NOT NULL AUTO_INCREMENT,
`ridetms_ride_fk` int(11) DEFAULT NULL,
`ridetms_date_added` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`ridetms_depart_date` date NOT NULL DEFAULT '0000-00-00',
`ridetms_depart_time` tinyint(4) DEFAULT '0',
`ridetms_return` tinyint(1) DEFAULT '0',
`ridetms_return_date` date NOT NULL DEFAULT '0000-00-00',
`ridetms_return_time` tinyint(4) DEFAULT '0',
PRIMARY KEY (`ridetms_id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
CREATE TABLE `user_accounts` (
`uacc_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`uacc_group_fk` smallint(5) unsigned NOT NULL,
`uacc_email` varchar(100) NOT NULL,
`uacc_username` varchar(15) NOT NULL,
`uacc_password` varchar(60) NOT NULL,
`uacc_ip_address` varchar(40) NOT NULL,
`uacc_salt` varchar(40) NOT NULL,
`uacc_activation_token` varchar(40) NOT NULL,
`uacc_forgotten_password_token` varchar(40) NOT NULL,
`uacc_forgotten_password_expire` datetime NOT NULL,
`uacc_update_email_token` varchar(40) NOT NULL,
`uacc_update_email` varchar(100) NOT NULL,
`uacc_active` tinyint(1) unsigned NOT NULL,
`uacc_suspend` tinyint(1) unsigned NOT NULL,
`uacc_fail_login_attempts` smallint(5) NOT NULL,
`uacc_fail_login_ip_address` varchar(40) NOT NULL,
`uacc_date_fail_login_ban` datetime NOT NULL COMMENT 'Time user is banned until due to repeated failed logins',
`uacc_date_last_login` datetime NOT NULL,
`uacc_date_added` datetime NOT NULL,
PRIMARY KEY (`uacc_id`),
UNIQUE KEY `uacc_id` (`uacc_id`),
KEY `uacc_group_fk` (`uacc_group_fk`),
KEY `uacc_email` (`uacc_email`),
KEY `uacc_username` (`uacc_username`),
KEY `uacc_fail_login_ip_address` (`uacc_fail_login_ip_address`)
) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=latin1;
CREATE TABLE `user_profiles` (
`upro_id` int(11) NOT NULL AUTO_INCREMENT,
`upro_uacc_fk` int(11) NOT NULL,
`upro_name` varchar(100) DEFAULT NULL,
`upro_blackberry_id` varchar(200) DEFAULT NULL,
`upro_yahoo_id` varchar(200) DEFAULT NULL,
`upro_skype_id` varchar(200) DEFAULT NULL,
`upro_gmail_id` varchar(200) DEFAULT NULL,
`upro_image_name` varchar(200) DEFAULT 'default.jpg',
PRIMARY KEY (`upro_id`),
UNIQUE KEY `upro_id` (`upro_id`),
KEY `upro_uacc_fk` (`upro_uacc_fk`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=latin1;
So, to clarify:
Right now I am pulling some text from depart_times based on ride_times.ridetms_depart_time. I need to also pull some text form depart_times based on ride_times.ridetms_return_time.
I think if you have joined all the tables already, Just put the condition with the WHERE clause Like :
WHERE
ride_times.ridetms_id = ?
AND
depart_times.column_name1 = ride_times.return_time
Does this answer your question?
Well, I must point out this line:
LEFT JOIN depart_times ON depart_times.dpttme_id = ride_times.ridetms_depart_time
Just feels like, something wrong with the schema. But, that is also a guess from my side seeing the namings..
Ok,
After LOTS more searching, I found this post:
MySQL alias for SELECT * columns
I have revised my statement to the following and it is working properly:
SELECT user_accounts.uacc_id,
user_accounts.uacc_username,
ride_rides.*,
user_profiles.upro_image_name,
ride_times.*,
dpt1.dpttme_text AS dep_text,
dpt2.dpttme_text AS ret_text
FROM ride_times
LEFT JOIN ride_rides
ON ride_rides.ride_id = ride_times.ridetms_ride_fk
LEFT JOIN user_accounts
ON ride_rides.ride_uacc_fk = user_accounts.uacc_id
LEFT JOIN user_profiles
ON user_profiles.upro_uacc_fk = user_accounts.uacc_id
LEFT JOIN depart_times AS dpt1
ON dpt1.dpttme_id = ride_times.ridetms_depart_time
LEFT JOIN depart_times AS dpt2
ON dpt2.dpttme_id = ride_times.ridetms_return_time
WHERE ride_times.ridetms_id = ?
Thanks very much to everyone who attempted to help me.
take care,
lee