In the table calendar_event there can be several calendar events (inspections). At present my query returns the oldest inspection (oldest if we rank them by the field ce.calendar_event_start), I want it to return the newest (latest) inspection.
I'm struggling with the approach to this. I've tried adding MAX around ce.calendar_event_start but that doesn't work. I'm assuming I have to change the LEFT JOIN but I can't figure out how I'd change it so that it's joining on the newest ce.calendar_event_start value.
UPDATE 1 - Thanks to #Vinit
Changed the LEFT JOIN as per the below but the query fails to execute;
LEFT JOIN calendar_event ce
on ce.calendar_event_tenancy = t.tenancy_id
AND ce.calendar_event_id = (SELECT calendar_event_id FROM
calendar_event ceInner
ceInner.calendar_event_tenancy = t.tenancy_id
AND ce.calendar_event_type='7'
AND ce.calendar_event_status!='3'
ORDER BY ceInner.calendar_event_start DESC)
UPDATE 2 - I worked on #Vinit answer to come up with the below that now works! Could this be improved any further?
Changed the LEFT JOIN as per the below but the query fails to execute;
LEFT JOIN calendar_event ce
on ce.calendar_event_tenancy = t.tenancy_id AND ce.calendar_event_id = (SELECT ceInner.calendar_event_id FROM
calendar_event ceInner
WHERE ceInner.calendar_event_tenancy = t.tenancy_id
AND ceInner.calendar_event_type='7'
AND ceInner.calendar_event_status!='3'
ORDER BY ceInner.calendar_event_start DESC
LIMIT 1)
UPDATE 3 - I've noticed the query takes a very long time to execute (approx 10 seconds). Is there a more efficient version of the same thing? Yes there is! See below;
LEFT JOIN calendar_event ce
on ce.calendar_event_tenancy = t.tenancy_id AND ce.calendar_event_start = (SELECT MAX(ceInner.calendar_event_start) FROM
calendar_event ceInner
WHERE ceInner.calendar_event_tenancy = t.tenancy_id
AND ceInner.calendar_event_type='7'
AND ceInner.calendar_event_status!='3'
AND ceInner.calendar_event_inspection_type!='2')
Query;
SELECT t.*, ce.*, IF(ce.calendar_event_start IS NOT NULL, ce.calendar_event_start, t.tenancy_start_date) AS LastInspection
FROM tenancy t
LEFT JOIN calendar_event ce
on ce.calendar_event_tenancy = t.tenancy_id AND ce.calendar_event_type='7' AND ce.calendar_event_status!='3'
ORDER BY LastInspection ASC
Tables & Sample Data;
-- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Jul 19, 2018 at 07:57 PM
-- Server version: 5.6.39
-- PHP Version: 5.6.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `propsyst_main`
--
-- --------------------------------------------------------
--
-- Table structure for table `calendar_event`
--
CREATE TABLE `calendar_event` (
`calendar_event_id` int(11) NOT NULL,
`calendar_event_company_id` int(11) DEFAULT NULL,
`calendar_event_branch_id` int(11) DEFAULT NULL,
`calendar_event_subject` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`calendar_event_status` tinyint(4) DEFAULT NULL,
`calendar_event_start` datetime DEFAULT NULL,
`calendar_event_end` datetime DEFAULT NULL,
`calendar_event_location` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`calendar_event_type` tinyint(4) DEFAULT NULL,
`calendar_event_employee` int(11) DEFAULT NULL,
`calendar_event_description` text COLLATE utf8_bin,
`calendar_event_attendee_type` int(11) DEFAULT NULL,
`calendar_event_property` int(11) DEFAULT NULL,
`calendar_event_tenancy` int(11) DEFAULT NULL,
`calendar_event_applicant` int(11) DEFAULT NULL,
`calendar_event_valuation` int(11) DEFAULT NULL,
`calendar_event_meet_at` tinyint(4) DEFAULT NULL,
`calendar_event_date_created` datetime DEFAULT NULL,
`calendar_event_date_updated` datetime DEFAULT NULL,
`calendar_event_created_by` int(11) DEFAULT NULL,
`calendar_event_updated_by` int(11) DEFAULT NULL,
`calendar_event_feedback_position` text COLLATE utf8_bin,
`calendar_event_feedback_pros` text COLLATE utf8_bin,
`calendar_event_feedback_cons` text COLLATE utf8_bin,
`calendar_event_feedback_confidence_level` tinyint(4) DEFAULT NULL,
`calendar_event_feedback_public_comments` text COLLATE utf8_bin,
`calendar_event_feedback_private_comments` text COLLATE utf8_bin,
`calendar_event_confirmed_landlord_vendor` tinyint(4) DEFAULT NULL,
`calendar_event_confirmed_applicant` tinyint(4) DEFAULT NULL,
`calendar_event_notes` text COLLATE utf8_bin,
`calendar_event_private_notes` text COLLATE utf8_bin,
`calendar_event_branch` int(11) DEFAULT NULL,
`calendar_event_recurring_id` int(11) DEFAULT NULL,
`calendar_event_cancellation_reason` text COLLATE utf8_bin,
`calendar_event_surveyor_company_name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`calendar_event_surveyor_individual_name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`calendar_event_surveyor_phone_number` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`calendar_event_directory` int(11) DEFAULT NULL,
`calendar_event_inspection_type` tinyint(1) DEFAULT NULL,
`calendar_event_survey_type` tinyint(4) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `calendar_event`
--
INSERT INTO `calendar_event` (`calendar_event_id`, `calendar_event_company_id`, `calendar_event_branch_id`, `calendar_event_subject`, `calendar_event_status`, `calendar_event_start`, `calendar_event_end`, `calendar_event_location`, `calendar_event_type`, `calendar_event_employee`, `calendar_event_description`, `calendar_event_attendee_type`, `calendar_event_property`, `calendar_event_tenancy`, `calendar_event_applicant`, `calendar_event_valuation`, `calendar_event_meet_at`, `calendar_event_date_created`, `calendar_event_date_updated`, `calendar_event_created_by`, `calendar_event_updated_by`, `calendar_event_feedback_position`, `calendar_event_feedback_pros`, `calendar_event_feedback_cons`, `calendar_event_feedback_confidence_level`, `calendar_event_feedback_public_comments`, `calendar_event_feedback_private_comments`, `calendar_event_confirmed_landlord_vendor`, `calendar_event_confirmed_applicant`, `calendar_event_notes`, `calendar_event_private_notes`, `calendar_event_branch`, `calendar_event_recurring_id`, `calendar_event_cancellation_reason`, `calendar_event_surveyor_company_name`, `calendar_event_surveyor_individual_name`, `calendar_event_surveyor_phone_number`, `calendar_event_directory`, `calendar_event_inspection_type`, `calendar_event_survey_type`) VALUES
(5170, 100, 0, 'Property Inspection (Rent Arrears) with Ms xxx xxx at Apartment 1 1 Howard Drive, Aigburth, Liverpool, L19 XXX', 2, '2017-07-28 14:30:00', '2017-07-28 14:45:00', 'Apartment 1 1 Howard Drive, Aigburth, Liverpool, L19 XXX', 7, 1, 'Tenant(s): Ms xxx xxx (Mobile: 00000000000, Email: no.email#mailinator.com)\r\nMeeting with: Agent (Michael Le Brocq)\r\nInspection Type: Rent Arrears\r\nPublic Notes: Property Inspection due to late rent\r\nStatus: Confirmed\r\nOriginally Arranged: 28/07/17 11:45:19 by Jane Nicholson\r\nLast Updated: 19/07/18 16:00:59 by Michael Le Brocq', 1, 172, 82, 0, 0, 0, '2017-07-28 11:45:19', '2018-07-19 16:00:59', 33, 1, '', NULL, NULL, NULL, NULL, NULL, 0, 0, 'Property Inspection due to late rent', '', 1, NULL, '', '', '', '', 0, 2, 0),
(5931, 100, 0, 'Property Inspection (Rent Arrears) with Ms Susan XXX at Apartment 1 1 Howard Drive, Aigburth, Liverpool, L19 XXX', 2, '2017-10-30 12:00:00', '2017-10-30 12:30:00', 'Apartment 1 1 Howard Drive, Aigburth, Liverpool, L19 XXX', 7, 1, 'Tenant(s): Ms Susan XXX (Mobile: 00000000000, Email: no.email#mailinator.com)\r\nMeeting with: Agent (Michael Le Brocq)\r\nInspection Type: Rent Arrears\r\nPublic Notes: Tenant not paid the rent\r\nStatus: Confirmed\r\nOriginally Arranged: 25/10/17 13:11:32 by Jane Nicholson\r\nLast Updated: 19/07/18 16:02:50 by Michael Le Brocq', 1, 172, 82, 0, 0, 0, '2017-10-25 13:11:32', '2018-07-19 16:02:50', 33, 1, '', NULL, NULL, NULL, NULL, NULL, 0, 0, 'Tenant not paid the rent', '', 1, NULL, '', '', '', '', 0, 2, 0),
(8929, 100, 0, 'Property Inspection (Routine) with Ms Susan XXX at Apartment 1 1 Howard Drive, Aigburth, Liverpool, L19 XXX', 2, '2018-07-25 15:00:00', '2018-07-25 15:15:00', 'Apartment 1 1 Howard Drive, Aigburth, Liverpool, L19 XXX', 7, 36, 'Tenant(s): Ms Susan XXX (Mobile: 00000000000, Email: no.email#mailinator.com)\r\nMeeting with: Agent (Jan Borrows)\r\nInspection Type: Routine\r\nPublic Notes: Confirmed with Diane\r\nStatus: Confirmed\r\nOriginally Arranged: 17/07/18 15:37:54 by Jan Borrows\r\nLast Updated: 17/07/18 15:37:54 by Jan Borrows', 1, 172, 82, 0, 0, 0, '2018-07-17 15:37:54', '2018-07-17 15:37:54', 36, 36, '', NULL, NULL, NULL, NULL, NULL, 0, 0, 'Confirmed with Diane', '', 1, NULL, NULL, '', '', '', 0, 1, 0);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `calendar_event`
--
ALTER TABLE `calendar_event`
ADD PRIMARY KEY (`calendar_event_id`),
ADD UNIQUE KEY `calendar_event_id` (`calendar_event_id`),
ADD KEY `calendar_event_status` (`calendar_event_status`),
ADD KEY `calendar_event_start` (`calendar_event_start`),
ADD KEY `calendar_event_end` (`calendar_event_end`),
ADD KEY `calendar_event_property` (`calendar_event_property`),
ADD KEY `calendar_event_employee` (`calendar_event_employee`),
ADD KEY `calendar_event_type` (`calendar_event_type`),
ADD KEY `calendar_event_company_id` (`calendar_event_company_id`),
ADD KEY `calendar_event_branch_id` (`calendar_event_branch_id`),
ADD KEY `calendar_event_attendee_type` (`calendar_event_attendee_type`),
ADD KEY `calendar_event_applicant` (`calendar_event_applicant`),
ADD KEY `calendar_event_valuation` (`calendar_event_valuation`),
ADD KEY `calendar_event_meet_at` (`calendar_event_meet_at`),
ADD KEY `calendar_event_branch` (`calendar_event_branch`),
ADD KEY `calendar_event_recurring_id` (`calendar_event_recurring_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `calendar_event`
--
ALTER TABLE `calendar_event`
MODIFY `calendar_event_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8968;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
AND
-- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Jul 19, 2018 at 07:58 PM
-- Server version: 5.6.39
-- PHP Version: 5.6.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `propsyst_main`
--
-- --------------------------------------------------------
--
-- Table structure for table `tenancy`
--
CREATE TABLE `tenancy` (
`tenancy_id` int(11) NOT NULL,
`tenancy_token` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`tenancy_type` tinyint(4) DEFAULT NULL,
`tenancy_property` int(11) DEFAULT NULL,
`tenancy_furnished` int(11) DEFAULT NULL,
`tenancy_children` tinyint(4) DEFAULT NULL,
`tenancy_pets` tinyint(4) DEFAULT NULL,
`tenancy_smoking` tinyint(4) NOT NULL,
`tenancy_property_part` tinyint(4) DEFAULT NULL,
`tenancy_property_part_room` int(11) DEFAULT NULL,
`tenancy_property_part_description` text COLLATE utf8_bin,
`tenancy_shared_facilities` tinyint(4) DEFAULT NULL,
`tenancy_shared_facilities_description` text COLLATE utf8_bin,
`tenancy_agreement_date` date DEFAULT NULL,
`tenancy_start_date` date DEFAULT NULL,
`tenancy_fixed_term` smallint(6) DEFAULT NULL,
`tenancy_fixed_term_unit` tinyint(4) DEFAULT NULL,
`tenancy_fixed_term_end_date` date DEFAULT NULL,
`tenancy_rent_amount` decimal(8,2) DEFAULT NULL,
`tenancy_rent_frequency` int(11) DEFAULT NULL,
`tenancy_rent_payable` int(11) DEFAULT NULL,
`tenancy_rent_agreement` int(11) DEFAULT NULL,
`tenancy_rent_frequency_schedule` text CHARACTER SET utf8,
`tenancy_rent_payment_method` tinyint(4) DEFAULT NULL,
`tenancy_council_pay_rent` tinyint(4) DEFAULT NULL,
`tenancy_rent_vat_rate` tinyint(4) DEFAULT NULL,
`tenancy_service_charge_amount` decimal(8,2) DEFAULT NULL,
`tenancy_service_charge_frequency` int(11) DEFAULT NULL,
`tenancy_service_charge_payable` int(11) DEFAULT NULL,
`tenancy_service_charge_agreement` int(11) DEFAULT NULL,
`tenancy_service_charge_frequency_schedule` text COLLATE utf8_bin,
`tenancy_service_charge_payment_method` tinyint(4) DEFAULT NULL,
`tenancy_service_charge_vat_rate` tinyint(4) DEFAULT NULL,
`tenancy_insurance_amount` decimal(8,2) DEFAULT NULL,
`tenancy_insurance_frequency` int(11) DEFAULT NULL,
`tenancy_insurance_payable` int(11) DEFAULT NULL,
`tenancy_insurance_agreement` int(11) DEFAULT NULL,
`tenancy_insurance_frequency_schedule` text COLLATE utf8_bin,
`tenancy_insurance_payment_method` tinyint(4) DEFAULT NULL,
`tenancy_insurance_vat_rate` tinyint(4) DEFAULT NULL,
`tenancy_notes` text COLLATE utf8_bin,
`tenancy_agent_branch` int(11) DEFAULT NULL,
`tenancy_agent_employee` int(11) DEFAULT NULL,
`tenancy_letting_service` tinyint(4) DEFAULT NULL,
`tenancy_tenant_find_fee` decimal(10,2) DEFAULT NULL,
`tenancy_tenant_find_fee_type` tinyint(4) DEFAULT NULL,
`tenancy_management_fee` decimal(10,2) DEFAULT NULL,
`tenancy_management_fee_type` tinyint(4) DEFAULT NULL,
`tenancy_tenant_fee` decimal(6,2) DEFAULT NULL,
`tenancy_guarantor_fee` decimal(6,2) DEFAULT NULL,
`tenancy_reminder_letter_fee` decimal(6,2) DEFAULT NULL,
`tenancy_missed_payment_fee` decimal(6,2) DEFAULT NULL,
`tenancy_gas` tinyint(4) DEFAULT NULL,
`tenancy_electricity` tinyint(4) DEFAULT NULL,
`tenancy_water` tinyint(4) DEFAULT NULL,
`tenancy_oil` tinyint(4) DEFAULT NULL,
`tenancy_telephone` tinyint(4) DEFAULT NULL,
`tenancy_broadband` tinyint(4) DEFAULT NULL,
`tenancy_tv_licence` tinyint(4) DEFAULT NULL,
`tenancy_sat_cable_tv` tinyint(4) DEFAULT NULL,
`tenancy_council_tax` tinyint(4) DEFAULT NULL,
`tenancy_service_charge` tinyint(4) DEFAULT NULL,
`tenancy_ground_rent` tinyint(4) DEFAULT NULL,
`tenancy_deposit_required` tinyint(4) DEFAULT NULL,
`tenancy_deposit_amount` decimal(8,2) DEFAULT NULL,
`tenancy_deposit_protection_responsible` tinyint(4) DEFAULT NULL,
`tenancy_deposit_protection_scheme` tinyint(4) DEFAULT NULL,
`tenancy_status` tinyint(4) DEFAULT NULL,
`tenancy_renewal_status` tinyint(4) DEFAULT '4',
`tenancy_renewal_notes` text COLLATE utf8_bin,
`tenancy_move_out_date` date DEFAULT NULL,
`tenancy_move_out_reason` tinyint(4) DEFAULT NULL,
`tenancy_move_out_notes` text COLLATE utf8_bin,
`tenancy_tenant_find_with_management_fee` decimal(7,2) DEFAULT NULL,
`tenancy_tenant_find_with_management_fee_type` tinyint(4) DEFAULT NULL,
`tenancy_overdue_tc_reminders` tinyint(4) NOT NULL DEFAULT '1',
`tenancy_student` tinyint(4) DEFAULT NULL,
`tenancy_inspection_frequency` tinyint(4) DEFAULT NULL,
`tenancy_move_out_employee` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `tenancy`
--
INSERT INTO `tenancy` (`tenancy_id`, `tenancy_token`, `tenancy_type`, `tenancy_property`, `tenancy_furnished`, `tenancy_children`, `tenancy_pets`, `tenancy_smoking`, `tenancy_property_part`, `tenancy_property_part_room`, `tenancy_property_part_description`, `tenancy_shared_facilities`, `tenancy_shared_facilities_description`, `tenancy_agreement_date`, `tenancy_start_date`, `tenancy_fixed_term`, `tenancy_fixed_term_unit`, `tenancy_fixed_term_end_date`, `tenancy_rent_amount`, `tenancy_rent_frequency`, `tenancy_rent_payable`, `tenancy_rent_agreement`, `tenancy_rent_frequency_schedule`, `tenancy_rent_payment_method`, `tenancy_council_pay_rent`, `tenancy_rent_vat_rate`, `tenancy_service_charge_amount`, `tenancy_service_charge_frequency`, `tenancy_service_charge_payable`, `tenancy_service_charge_agreement`, `tenancy_service_charge_frequency_schedule`, `tenancy_service_charge_payment_method`, `tenancy_service_charge_vat_rate`, `tenancy_insurance_amount`, `tenancy_insurance_frequency`, `tenancy_insurance_payable`, `tenancy_insurance_agreement`, `tenancy_insurance_frequency_schedule`, `tenancy_insurance_payment_method`, `tenancy_insurance_vat_rate`, `tenancy_notes`, `tenancy_agent_branch`, `tenancy_agent_employee`, `tenancy_letting_service`, `tenancy_tenant_find_fee`, `tenancy_tenant_find_fee_type`, `tenancy_management_fee`, `tenancy_management_fee_type`, `tenancy_tenant_fee`, `tenancy_guarantor_fee`, `tenancy_reminder_letter_fee`, `tenancy_missed_payment_fee`, `tenancy_gas`, `tenancy_electricity`, `tenancy_water`, `tenancy_oil`, `tenancy_telephone`, `tenancy_broadband`, `tenancy_tv_licence`, `tenancy_sat_cable_tv`, `tenancy_council_tax`, `tenancy_service_charge`, `tenancy_ground_rent`, `tenancy_deposit_required`, `tenancy_deposit_amount`, `tenancy_deposit_protection_responsible`, `tenancy_deposit_protection_scheme`, `tenancy_status`, `tenancy_renewal_status`, `tenancy_renewal_notes`, `tenancy_move_out_date`, `tenancy_move_out_reason`, `tenancy_move_out_notes`, `tenancy_tenant_find_with_management_fee`, `tenancy_tenant_find_with_management_fee_type`, `tenancy_overdue_tc_reminders`, `tenancy_student`, `tenancy_inspection_frequency`, `tenancy_move_out_employee`) VALUES
(82, 'aJ9GCrxk9IEkBkLUqxsTuIdlbHnGNXTc4coq69AW', 1, 172, 2, 0, 0, 0, 1, 0, '', 0, '', '2014-08-21', '2014-08-21', 6, 2, '2015-02-20', '395.00', 3, 3, 1, '', 3, 0, 0, '0.00', 0, 0, 0, '', 0, 0, '0.00', 0, 0, 0, '', 0, 0, '', 1, 1, 3, '225.00', 1, '10.00', 2, '70.00', '50.00', '5.00', '15.00', 2, 2, 2, 3, 2, 2, 2, 2, 2, 3, 3, 1, '500.00', 1, 1, 1, 1, NULL, '0000-00-00', 0, '', '239.00', 1, 1, 0, 12, NULL);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tenancy`
--
ALTER TABLE `tenancy`
ADD PRIMARY KEY (`tenancy_id`),
ADD UNIQUE KEY `tenancy_token` (`tenancy_token`),
ADD KEY `tenancy_id` (`tenancy_id`),
ADD KEY `tenancy_property` (`tenancy_property`),
ADD KEY `tenancy_status` (`tenancy_status`),
ADD KEY `tenancy_letting_service` (`tenancy_letting_service`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `tenancy`
--
ALTER TABLE `tenancy`
MODIFY `tenancy_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=526;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
The order by on date column suppose to be descending to get latest inspection date
ORDER BY LastInspection DESC
EDIT
You may try this query -
SELECT t.*,
ce.*,
IF(
ce.calendar_event_start IS NOT NULL,
ce.calendar_event_start,
t.tenancy_start_date
) AS LastInspection
FROM tenancy t
LEFT JOIN calendar_event ce
on ce.calendar_event_tenancy = t.tenancy_id
AND ce.calendar_event_start = (select max(ceInner.calendar_event_start) from -- get the recent event id
calendar_event ceInner
ceInner.calendar_event_tenancy = t.tenancy_id
AND ceInner.calendar_event_type='7'
AND ceInner.calendar_event_status!='3'
)
ORDER BY LastInspection DESC
I received this error while uploading SQL to server phpMyAdmin. I did look at the answers from similar posts, but I don't think it's the same thing. It should be after -- Table structure for table wzflh_admintools_log. These are lines 127 through 146:
CREATE TABLE `wzflh_admintools_ipblock` (
`id` bigint(20) UNSIGNED NOT NULL,
`ip` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `wzflh_admintools_log`
--
CREATE TABLE `wzflh_admintools_log` (
`id` bigint(20) UNSIGNED NOT NULL,
`logdate` datetime NOT NULL,
`ip` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reason` enum('other','adminpw','ipwl','ipbl','sqlishield','antispam','wafblacklist','tpone','tmpl','template','muashield','csrfshield','badbehaviour','geoblocking','rfishield','dfishield','uploadshield','xssshield','httpbl','loginfailure','securitycode','external','awayschedule','admindir','sessionshield','nonewadmins','nonewfrontendadmins','configmonitor','phpshield') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`extradata` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
I have created a database named mydb and imported your tables via the console. This is what I have:
-- phpMyAdmin SQL Dump
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE `wzflh_admintools_ipblock` (
`id` bigint(20) UNSIGNED NOT NULL,
`ip` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `wzflh_admintools_log` (
`id` bigint(20) UNSIGNED NOT NULL,
`logdate` datetime NOT NULL,
`ip` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reason` enum('other', 'adminpw', 'ipwl', 'ipbl', 'sqlishield',
'antispam', 'wafblacklist', 'tpone', 'tmpl',
'template', 'muashield', 'csrfshield',
'badbehaviour', 'geoblocking', 'rfishield',
'dfishield', 'uploadshield', 'xssshield', 'httpbl',
'loginfailure', 'securitycode', 'external',
'awayschedule', 'admindir', 'sessionshield',
'nonewadmins', 'nonewfrontendadmins', 'configmonitor',
'phpshield') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`extradata` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Copy and paste this to your SQL file:
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Oct 19, 2017 at 07:46 PM
-- Server version: 10.1.16-MariaDB
-- PHP Version: 5.6.24
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `centeroakland`
--
-- --------------------------------------------------------
--
-- Table structure for table `wzflh_admintools_ipblock`
--
CREATE TABLE `wzflh_admintools_ipblock` (
`id` bigint(20) UNSIGNED NOT NULL,
`ip` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `wzflh_admintools_log`
--
CREATE TABLE `wzflh_admintools_log` (
`id` bigint(20) UNSIGNED NOT NULL,
`logdate` datetime NOT NULL,
`ip` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reason` enum('other','adminpw','ipwl','ipbl','sqlishield','antispam','wafblacklist','tpone','tmpl','template','muashield','csrfshield','badbehaviour','geoblocking','rfishield','dfishield','uploadshield','xssshield','httpbl','loginfailure','securitycode','external','awayschedule','admindir','sessionshield','nonewadmins','nonewfrontendadmins','configmonitor','phpshield') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`extradata` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
Save it then import it.
I have been looking everywhere for an answer, but I still have not found a solution.
Background:
I have a MySQL Server 5.5 running on my computer for testing data that will be eventually loaded into a Production Server for my company. I created the database in MySQL Workbench and have edited it where appropriate to fit my data as I loaded it. My data is nowhere near clean, so for the most part I am using VARCHAR for my fields, since a whole bunch of random things can be put into the field. I have tried changing multiple things and I am no longer sure whether anything I have done has fixed or even helped to eliminate the issue at all.
My Code:
DB Creation:
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
SET NAMES utf8;
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Customer_Account`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Customer_Account` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Customer_Account` (
`idCustomer_Account` VARCHAR(20) NOT NULL,
`Name` VARCHAR(130) NOT NULL,
`Billing_Street` VARCHAR(150) NULL,
`Billing_City` VARCHAR(30) NULL,
`Billing_StateProvince` VARCHAR(25) NULL,
`Billing_PostalCode` VARCHAR(15) NULL,
`Billing_Country` VARCHAR(20) NULL,
`Location_Type` VARCHAR(15) NULL,
`Parent_ID` VARCHAR(20) NULL,
`Parent_Name` VARCHAR(130) NULL,
PRIMARY KEY (`idCustomer_Account`),
UNIQUE INDEX `idCustomer_Account_UNIQUE` (`idCustomer_Account` ASC))
ENGINE = InnoDB DEFAULT CHARSET=utf8;
'CONSTRAINT `Parent_ID`
FOREIGN KEY (`idCustomer_Account`)
REFERENCES `mydb`.`Customer_Account` (`idCustomer_Account`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)'
-- -----------------------------------------------------
-- Table `mydb`.`Customer_Address`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Customer_Address` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Customer_Address` (
`idCustomer_Address` VARCHAR(20) NOT NULL,
`Name` VARCHAR(50) NOT NULL,
`Name_Int` INT,
`Address` VARCHAR(80) NULL,
`City` VARCHAR(30) NULL,
`State` VARCHAR(5) NULL,
`ZIP` VARCHAR(15) NULL,
`LinkedAccount_Name` VARCHAR(100) NULL,
`LinkedAccount_ID` VARCHAR(20) NULL,
`CADD` INT,
PRIMARY KEY (`idCustomer_Address`),
UNIQUE INDEX `idCustomer_Address_UNIQUE` (`idCustomer_Address` ASC))
ENGINE = InnoDB DEFAULT CHARSET=utf8;
-- -----------------------------------------------------
-- Table `mydb`.`Contacts`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Contacts` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Contacts` (
`idContacts` VARCHAR(20) NOT NULL,
`Name` VARCHAR(50) NOT NULL,
`Title` VARCHAR(130) NULL,
`Mailing_Street` VARCHAR(110) NULL,
`Mailing_City` VARCHAR(30) NULL,
`Mailing_State` VARCHAR(25) NULL,
`Mailing_PostalCode` VARCHAR(20) NULL,
`Phone` VARCHAR(50) NULL,
`Account_ID` VARCHAR(20) NULL,
`Account_Name` VARCHAR(100) NULL,
PRIMARY KEY (`idContacts`),
UNIQUE INDEX `idContacts_UNIQUE` (`idContacts` ASC))
ENGINE = InnoDB DEFAULT CHARSET=utf8;
-- -----------------------------------------------------
-- Table `mydb`.`Contact_Details`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Contact_Details` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Contact_Details` (
`idContact_Details` VARCHAR(20) NOT NULL,
`Salutation` VARCHAR(70) NULL,
`First_Name` VARCHAR(30) NULL,
`Last_Name` VARCHAR(50) NULL,
`Title` VARCHAR(130) NULL,
`Mailing_Street` VARCHAR(175) NULL,
`Mailing_City` VARCHAR(50) NULL,
`Mailing_StateProvince` VARCHAR(30) NULL,
`Mailing_PostalCode` VARCHAR(20) NULL,
`Mailing_Country` VARCHAR(25) NULL,
`Phone` VARCHAR(50) NULL,
`Mobile` VARCHAR(35) NULL,
`Fax` VARCHAR(45) NULL,
`Email` VARCHAR(75) NULL,
`Email_OptOut` VARCHAR(1) NULL,
`Account_Owner` VARCHAR(20) NULL,
`Account_Name` VARCHAR(125) NULL,
`Account_ID` VARCHAR(20) NOT NULL,
`Active` VARCHAR(2) NOT NULL,
PRIMARY KEY (`idContact_Details`),
UNIQUE INDEX `idContact_Details_UNIQUE` (`idContact_Details` ASC))
ENGINE = InnoDB DEFAULT CHARSET=utf8;
'INDEX `Account_ID_idx` (`Account_ID` ASC)'
-- -----------------------------------------------------
-- Table `mydb`.`WF1_SR_Accounts`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`WF1_SR_Accounts` ;
CREATE TABLE IF NOT EXISTS `mydb`.`WF1_SR_Accounts` (
`idWF1_SR_Accounts` VARCHAR(20) NOT NULL,
`Name` VARCHAR(125) NOT NULL,
`Billing_Street` VARCHAR(135) NULL,
`Billing_City` VARCHAR(50) NULL,
`Billing_State` VARCHAR(20) NULL,
`Billing_PostalCode` VARCHAR(20) NULL,
`Billing_Country` VARCHAR(25) NULL,
`Location_Type` VARCHAR(20) NULL,
`Parent_ID` VARCHAR(20) NULL,
`Parent_Name` VARCHAR(125) NULL,
`SR_Account?` VARCHAR(5) NULL,
`WF1_Account?` VARCHAR(5) NULL,
`WF1_AccountNum` VARCHAR(10) NULL,
`WF1_AccountStatus` VARCHAR(25) NULL,
`WF1_AccountCreated` VARCHAR(50) NULL,
`WF1_BU` VARCHAR(10) NULL,
`WF1_Collector` VARCHAR(20) NULL,
`WF1_CreditHold` VARCHAR(5) NULL,
`WF1_CreditLimit` VARCHAR(10) NULL,
`WF1_CreditRating` VARCHAR(15) NULL,
`WF1_DVP` VARCHAR(20) NULL,
`WF1_PaymentTerms` VARCHAR(20) NULL,
`WF1_RSD` VARCHAR(40) NULL,
`WF1_TerritoryName` VARCHAR(45) NULL,
`WF1_TerritoryNumber` VARCHAR(5) NULL,
`Select_` VARCHAR(5) NULL,
`New_Select` VARCHAR(5) NULL,
`CRMFusion_Use` VARCHAR(10) NULL,
`CRMFusion_Change` VARCHAR(10) NULL,
PRIMARY KEY (`idWF1_SR_Accounts`),
UNIQUE INDEX `idWF1_SR_Accounts_UNIQUE` (`idWF1_SR_Accounts` ASC))
ENGINE = InnoDB DEFAULT CHARSET=utf8;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
Note: I have also substituted latin1 for utf8 in SET NAMES, DEFUALT CHARSET, and CHARACTER_SET_SERVER, and tried to make my .txt files of both encryption types.
I also ran
SHOW VARIABLES like 'char%';
and this is my result:
Variable_name Value
character_set_client latin1
character_set_connection latin1
character_set_database utf8
character_set_filesystem binary
character_set_results latin1
character_set_server latin1
character_set_system utf8
character_sets_dir C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\
Note: at one point, everything but filesystem and server were utf8 and it did not work either.
I have also changed the encryption type in excel and notepad++ and changing that did not seem to help.
The primary Key need to be changed from INT to VARCHAR(45) data types.