Err 1292 - Truncated incorrect DOUBLE value - mysql

I have this two tables:
OLD:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`crdate` int(11) DEFAULT NULL,
`fb_id` text,
`email` varchar(64) DEFAULT NULL,
`fb_access_token` varchar(256) DEFAULT NULL,
`display_name` varchar(128) DEFAULT NULL,
`first_name` varchar(128) DEFAULT NULL,
`middle_name` varchar(128) DEFAULT NULL,
`last_name` varchar(128) DEFAULT NULL,
`gender` varchar(128) DEFAULT NULL,
`timezone` tinyint(4) DEFAULT NULL,
`locale` varchar(16) DEFAULT NULL,
`fb_profile_url` text,
`balanced_id` text,
`token` varchar(100) DEFAULT NULL,
`address_id` int(11) DEFAULT NULL,
`admin` tinyint(1) DEFAULT '0',
`zip` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
NEW:
CREATE TABLE IF NOT EXISTS `friendzy`.`users_new` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`email` VARCHAR(64) NULL DEFAULT NULL,
`display_name` VARCHAR(128) NULL DEFAULT NULL,
`first_name` VARCHAR(128) NULL DEFAULT NULL,
`middle_name` VARCHAR(128) NULL DEFAULT NULL,
`last_name` VARCHAR(128) NULL DEFAULT NULL,
`gender` VARCHAR(128) NULL DEFAULT NULL,
`redis_token` VARCHAR(100) NULL DEFAULT NULL,
`admin` TINYINT(1) NULL DEFAULT '0',
`api_token` VARCHAR(255) NULL DEFAULT NULL,
`password` VARCHAR(255) NOT NULL,
`salt` VARCHAR(255) NOT NULL,
`enabled` TINYINT(1) NOT NULL DEFAULT '0',
`last_login` TIMESTAMP NULL DEFAULT NULL,
`locked` TINYINT(1) NULL DEFAULT NULL,
`expired` TINYINT(1) NOT NULL DEFAULT '0',
`expired_at` TIMESTAMP NULL DEFAULT NULL,
`confirmation_token` VARCHAR(255) NULL DEFAULT NULL,
`password_requested_at` TIMESTAMP NULL DEFAULT NULL,
`credentials_expired` TINYINT(1) NULL DEFAULT NULL,
`credentials_expire_at` TIMESTAMP NULL DEFAULT NULL,
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`modified` TIMESTAMP NULL DEFAULT NULL,
`modified_by` VARCHAR(255) NULL DEFAULT NULL,
`deleted_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
AUTO_INCREMENT = 164
DEFAULT CHARACTER SET = latin1;
And im trying yo copy the data from the old one to the new one with this sentence:
INSERT INTO `users_new` (`id`, `email`, `display_name`, `first_name`, `middle_name`, `last_name`, `gender`, `redis_token`, `admin`, `api_token`, `password`, `salt`, `enabled`, `last_login`, `locked`, `expired`, `expired_at`, `confirmation_token`, `password_requested_at`, `credentials_expired`, `credentials_expire_at`, `created`, `modified`, `modified_by`, `deleted_at`)
SELECT `users`.`id`, `users`.`email`, `users`.`display_name`, `users`.`first_name`, `users`.`middle_name`, `users`.`last_name`, `users`.`gender`, if(`users`.`token`, `users`.`token`, md5(1)), `users`.`admin`, md5(1), md5(1), md5(1), 1, NULL, 0, 0, NULL, md5(1), NULL, 0, NULL, NOW(), NOW(), 'UPDATE SCRIPT', NULL
FROM users;
But i get this error:
[Err] 1292 - Truncated incorrect DOUBLE value:
'4043a809f18ffc8be824064934797d3a'

This is one of those really crappy errors that gets thrown that might not have anything to do with the actual problem. My suggestion would be to change your
if(`users`.`token`, `users`.`token`, md5(1))
to
COALESCE(`users`.`token`, md5(1))
And see if it doesn't fix itself.

Related

mysql 'slow queries log' return a huge number of rows examined but explain seems ok

The sql query
# Query_time: 16.536276 Lock_time: 0.000159 Rows_sent: 756 Rows_examined: 8392194
SET timestamp=1555422526;
SELECT c.id AS c__id, c.company_id AS c__company_id,
c.ordinary_price AS c__ordinary_price, c.nights AS c__nights,
c.food_type AS c__food_type, c.period AS c__period,
c.period_desc AS c__period_desc, c.extra AS c__extra,
c.coupons_bought AS c__coupons_bought, c.coupon_price AS c__coupon_price,
c.coordinates AS c__coordinates, c.best_price AS c__best_price,
c.from_price AS c__from_price, c.end_datetime AS c__end_datetime,
c.hide_clock AS c__hide_clock, c.hide_discount AS c__hide_discount,
c.booking_hotel_id AS c__booking_hotel_id, c.title AS c__title,
c.option_people AS c__option_people, c.option_room AS c__option_room,
c.option_period AS c__option_period, c.city AS c__city,
c2.id AS c2__id, c2.people AS c2__people, c2.room AS c2__room,
( SELECT c8.url AS c8__url
FROM campaign_images c8
WHERE (c8.campaign_id = c.id
AND c8.photo_type = 'list')
ORDER BY c8.ordering ASC LIMIT 1
) AS c__0
FROM campaign c
LEFT JOIN campaign_options c2 ON c.id = c2.campaign_id
AND (c2.active = 1)
LEFT JOIN city_in_campaign c3 ON c.id = c3.campaign_id
LEFT JOIN city c4 ON c3.city_id = c4.id
LEFT JOIN company c5 ON c.company_id = c5.id
LEFT JOIN campaign_in_category c6 ON c.id = c6.campaign_id
LEFT JOIN campaign_in_group c7 ON c.id = c7.campaign_id
WHERE c.id IN ('13308', '13281', '13265')
AND (c.status IN ('published')
AND c.start_datetime <= NOW()
AND c.end_datetime >= NOW()
AND c5.id = '2111'
AND c.id != '14624'
AND (c7.group_id in (1)
OR c7.group_id is NULL
)
)
ORDER BY c.coupon_expire_datetime ASC;
create table campaign
CREATE TABLE `campaign` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`company_id` int(11) DEFAULT NULL,
`source_id` int(11) DEFAULT NULL,
`secondary_source_id` int(11) DEFAULT NULL,
`source_hotel_id` int(11) DEFAULT NULL,
`booking_hotel_id` int(11) DEFAULT NULL,
`booking_board_id` int(11) DEFAULT NULL,
`booking_rate` varchar(100) DEFAULT NULL,
`ordinary_price` decimal(9,2) NOT NULL,
`best_price` tinyint(1) NOT NULL DEFAULT '0',
`from_price` tinyint(1) NOT NULL DEFAULT '0',
`status` varchar(10) DEFAULT 'draft',
`type` varchar(20) DEFAULT 'other',
`deal_status` varchar(20) DEFAULT 'in_progress',
`coupon_price` decimal(9,2) NOT NULL,
`cosmote_discount` decimal(5,2) DEFAULT NULL,
`nights` int(11) NOT NULL DEFAULT '0',
`period` varchar(255) DEFAULT NULL,
`period_desc` varchar(1000) DEFAULT NULL,
`hide_period` tinyint(1) NOT NULL DEFAULT '0',
`food_type` varchar(100) DEFAULT NULL,
`stars` varchar(100) DEFAULT NULL,
`adults` tinyint(1) DEFAULT NULL,
`childs` tinyint(1) DEFAULT NULL,
`extra` varchar(255) DEFAULT NULL,
`best_point` varchar(511) DEFAULT NULL,
`extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_desc` varchar(500) DEFAULT NULL,
`high_season_extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`family_packages_desc` varchar(1000) DEFAULT NULL,
`coordinates` varchar(70) DEFAULT NULL,
`start_datetime` datetime DEFAULT NULL,
`end_datetime` datetime DEFAULT NULL,
`coupon_expire_datetime` datetime DEFAULT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
`city` varchar(255) DEFAULT NULL,
`min_coupons` int(11) NOT NULL DEFAULT '0',
`is_global` tinyint(4) NOT NULL DEFAULT '0',
`hide_clock` tinyint(1) NOT NULL DEFAULT '0',
`hide_discount` tinyint(1) NOT NULL DEFAULT '0',
`hide_purchases` tinyint(1) NOT NULL DEFAULT '0',
`booking_enabled` tinyint(1) NOT NULL DEFAULT '0',
`booking_phone` varchar(50) DEFAULT NULL,
`refresh` tinyint(1) NOT NULL DEFAULT '0',
`installments` tinyint(1) NOT NULL DEFAULT '1',
`receipt` enum('0','1') NOT NULL DEFAULT '0',
`newsletters_sent` tinyint(4) NOT NULL DEFAULT '0',
`max_coupons` int(11) NOT NULL DEFAULT '0',
`max_coupons_per_user` int(11) DEFAULT NULL,
`coupons_bought` int(11) unsigned NOT NULL DEFAULT '0',
`fake_orders` int(11) DEFAULT '0',
`title` varchar(255) DEFAULT NULL,
`newsletter_title` varchar(500) DEFAULT NULL,
`linkwise_title` varchar(500) DEFAULT NULL,
`option_title` varchar(255) DEFAULT NULL,
`option_title_en` varchar(255) DEFAULT NULL,
`option_people` varchar(150) DEFAULT NULL,
`option_room` varchar(150) DEFAULT NULL,
`option_period` varchar(150) DEFAULT NULL,
`name` varchar(1200) DEFAULT NULL,
`description` text,
`highlights` text,
`coupon_instructions` text,
`show_in_recent_deals` tinyint(4) NOT NULL DEFAULT '1',
`youtube_video_id` varchar(100) DEFAULT NULL,
`in_side` tinyint(4) NOT NULL DEFAULT '0',
`family` tinyint(1) NOT NULL DEFAULT '0',
`send_newsletter` tinyint(4) NOT NULL DEFAULT '1',
`resend_newsletter` tinyint(4) NOT NULL DEFAULT '0',
`modified_datetime` timestamp NULL DEFAULT NULL,
`created_datetime` datetime NOT NULL,
`ordering` int(11) unsigned DEFAULT '0',
PRIMARY KEY (`id`),
KEY `company_id_idx` (`company_id`),
KEY `user_id_idx` (`user_id`),
KEY `status_indx` (`status`),
KEY `str_dt_indx` (`start_datetime`),
KEY `end_dt_indx` (`end_datetime`),
KEY `side_indx` (`in_side`),
KEY `ord_indx` (`ordering`),
KEY `global_indx` (`is_global`),
KEY `act_indx` (`active`),
KEY `coup_expr_index` (`coupon_expire_datetime`)
) ENGINE=InnoDB AUTO_INCREMENT=14788 DEFAULT CHARSET=utf8
create table campaign_options
CREATE TABLE `campaign_options` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`coupons_bought` int(11) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL,
`name_en` varchar(255) DEFAULT NULL,
`people` varchar(100) DEFAULT NULL,
`room` varchar(100) DEFAULT NULL,
`food` varchar(100) DEFAULT NULL,
`period` varchar(100) DEFAULT NULL,
`coupon_price` decimal(9,2) NOT NULL,
`extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_price` decimal(9,2) DEFAULT '0.00',
`high_season_extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`modified_datetime` timestamp NULL DEFAULT NULL,
`created_datetime` datetime NOT NULL,
`main` tinyint(1) NOT NULL DEFAULT '0',
`family` tinyint(1) NOT NULL DEFAULT '0',
`active` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `active_indx` (`active`),
KEY `family_indx` (`family`),
KEY `main_indx` (`main`)
) ENGINE=InnoDB AUTO_INCREMENT=48990 DEFAULT CHARSET=utf8
create table city_in_campaign
CREATE TABLE `city_in_campaign` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) DEFAULT NULL,
`city_id` int(11) DEFAULT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `city_id_idx` (`city_id`),
KEY `order_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=227176 DEFAULT CHARSET=utf8
create table city
CREATE TABLE `city` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`is_active` tinyint(3) unsigned DEFAULT NULL,
`modified_datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `country_id_idx` (`country_id`),
KEY `is_active_indx` (`is_active`)
) ENGINE=InnoDB AUTO_INCREMENT=254 DEFAULT CHARSET=utf8
create table company
CREATE TABLE `company` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`ref_id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`description` text,
`logo` varchar(255) DEFAULT NULL,
`phone_number` varchar(100) DEFAULT NULL,
`address` varchar(200) DEFAULT NULL,
`coordinates` varchar(70) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`website` varchar(255) DEFAULT NULL,
`skype_name` varchar(50) DEFAULT NULL,
`icq_number` varchar(255) DEFAULT NULL,
`payment_information` text,
`extra1` text,
`extra2` text,
`extra3` text,
`video` varchar(500) DEFAULT NULL,
`checked` tinyint(1) NOT NULL DEFAULT '0',
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `checked_indx` (`checked`),
KEY `ordering_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=2519 DEFAULT CHARSET=utf8
create table campaign_in_category
CREATE TABLE `campaign_in_category` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`category_id` int(11) DEFAULT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
`created_datetime` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `category_id_idx` (`category_id`),
KEY `order_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=457080 DEFAULT CHARSET=utf8
create table campaign_in_group
CREATE TABLE `campaign_in_group` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) DEFAULT NULL,
`group_id` int(11) DEFAULT NULL,
`created_datetime` datetime DEFAULT NULL,
`modified_datetime` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `camp_group_indx` (`campaign_id`,`group_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1175 DEFAULT CHARSET=utf8
campaign total rows: 12,657
campaign_options total rows: 43,714
city_in_campaign total rows: 15,162
city total rows: 215
company total rows: 1,756
campaign_in_category total rows: 38,817
campaign_in_group total rows: 395
explain of the query
which ones have index
it looks you need composite index because file sorting.
usage:
CREATE INDEX index_name
ON table_name(c2,c3,c4);
You are getting above result because some time o/p of explain gives different result as response of query of prod.
For optimization of query you can't have a fix answer. it varies from case to case. As for this situation I think you need to perform performance testing of this query. You can do this by inserting some records in all referenced table and then checking it's performance using explain.
LEFT JOIN campaign_in_category c6 ON c.id = c6.campaign_id
seems to be totally useless. But the Optimizer may not realize it. Remove it and any other dead code.

How can I optimize view in MySQL?

Load take 60 sec, how can I optimize?
Explain struct:
explain tablename;
IMG : http://i.stack.imgur.com/TfbY3.png
Explain SQL
explain select * from tablename;
IMG : http://i.stack.imgur.com/jHI47.png
REAL DB
DROP TABLE IF EXISTS `afiliados`;
CREATE TABLE `afiliados` (
`id_afiliado` int(10) NOT NULL AUTO_INCREMENT,
`clave_panel` char(15) COLLATE latin1_spanish_ci DEFAULT NULL,
`clave_panel_ask` datetime DEFAULT NULL,
`id_trabajador` char(20) COLLATE latin1_spanish_ci DEFAULT NULL,
`persona` char(1) COLLATE latin1_spanish_ci DEFAULT 'H',
`Estado` int(10) NOT NULL DEFAULT '0',
`doc_fiscal` int(10) DEFAULT NULL,
`id_fiscal` char(20) COLLATE latin1_spanish_ci DEFAULT NULL,
`Nombre` char(25) COLLATE latin1_spanish_ci DEFAULT NULL,
`Apellidos` char(50) COLLATE latin1_spanish_ci DEFAULT NULL,
`fecha_nacimiento` datetime DEFAULT NULL,
`Telefono Principal` char(12) COLLATE latin1_spanish_ci DEFAULT NULL,
`Telefono Secundario` char(12) COLLATE latin1_spanish_ci DEFAULT NULL,
`Fecha_Alta` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Fecha_Aprobacion` datetime DEFAULT NULL,
`id_cuenta_aprobacion` int(11) DEFAULT NULL,
`Fecha_Documentacion` datetime DEFAULT NULL,
`Fecha_Alta_Sindicato` datetime DEFAULT NULL,
`Fecha_Baja_Sindicato` datetime DEFAULT NULL,
`Motivo_Baja_Sindicato` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Fecha_Desafiliacion` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Fax` char(12) COLLATE latin1_spanish_ci DEFAULT NULL,
`Direccion` char(100) COLLATE latin1_spanish_ci DEFAULT NULL,
`CP` char(5) COLLATE latin1_spanish_ci DEFAULT NULL,
`Ciudad` char(35) COLLATE latin1_spanish_ci DEFAULT NULL,
`Provincia` char(25) COLLATE latin1_spanish_ci DEFAULT NULL,
`Pais` char(2) COLLATE latin1_spanish_ci DEFAULT NULL,
`Nacionalidad` char(2) COLLATE latin1_spanish_ci NOT NULL DEFAULT '',
`Email` char(50) COLLATE latin1_spanish_ci DEFAULT NULL,
`Email_secundario` char(50) COLLATE latin1_spanish_ci DEFAULT NULL,
`Id_delegado` int(10) DEFAULT NULL,
`Comentario` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Interno` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Eliminado` tinyint(1) NOT NULL DEFAULT '0',
`Es_Delegado` tinyint(1) NOT NULL DEFAULT '0',
`id_cuenta` int(10) DEFAULT NULL,
`Fecha_mod` datetime DEFAULT NULL,
`mod_id_cuenta` int(10) DEFAULT NULL,
`noemail` tinyint(1) NOT NULL DEFAULT '0',
`noemail_public` tinyint(1) NOT NULL DEFAULT '0',
`noemail_fact` tinyint(1) NOT NULL DEFAULT '0',
`noti_alta` datetime DEFAULT NULL,
`noti_fact` datetime DEFAULT NULL,
`noti_baja` datetime DEFAULT NULL,
`fecha_star_facturacion` datetime DEFAULT NULL,
`fecha_proxima_facturacion` datetime DEFAULT NULL,
`ciclo_mensual_facturacion` int(10) unsigned NOT NULL DEFAULT '3',
`fecha_sync` datetime DEFAULT NULL,
`fecha_chk` datetime DEFAULT NULL,
`LOPD` tinyint(1) NOT NULL DEFAULT '0',
`ncarnet` int(10) DEFAULT NULL,
`id_caja` int(10) unsigned NOT NULL DEFAULT '0',
`caja_fecha_add` datetime DEFAULT NULL,
`id_cuenta_caja_add` int(10) DEFAULT '0',
`caja_fecha_del` datetime DEFAULT NULL,
`id_cuenta_caja_del` int(10) DEFAULT '0',
PRIMARY KEY (`id_afiliado`),
KEY `apellidos` (`Apellidos`),
KEY `Email` (`Email`),
KEY `es_delegado` (`Es_Delegado`),
KEY `id_caja` (`id_caja`),
KEY `id_cuenta` (`id_cuenta`),
KEY `id_delegado` (`Id_delegado`),
KEY `id_fiscal` (`id_fiscal`),
KEY `id_trabajador` (`id_trabajador`),
KEY `nombre` (`Nombre`)
) ENGINE=MyISAM AUTO_INCREMENT=4467 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=DYNAMIC;
INSERT INTO `afiliados` VALUES (194,NULL,NULL,'XXXXX','M',1,1,'XXXXX','Frank','Poland','1967-11-13 00:00:00','0000000','','2012-08-08 10:35:31','2012-08-08 21:05:59',0,'2012-08-09 00:00:00','2005-04-04 00:00:00',NULL,NULL,NULL,'','Adress','00000','Sevilla','Sevilla','es','es',NULL,NULL,NULL,NULL,NULL,0,0,NULL,NULL,NULL,0,0,0,NULL,NULL,NULL,NULL,NULL,3,NULL,NULL,0,NULL,0,NULL,NULL,NULL,NULL);
DROP TABLE IF EXISTS `contratos`;
CREATE TABLE `contratos` (
`Id_contrato` int(10) NOT NULL AUTO_INCREMENT,
`id_empresa` int(10) DEFAULT NULL,
`Marca` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Departamento` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`Localizacion` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`fecha_baja` datetime DEFAULT NULL,
`id_cuenta_add` int(10) DEFAULT NULL,
PRIMARY KEY (`Id_contrato`),
KEY `id_empresa` (`id_empresa`)
) ENGINE=MyISAM AUTO_INCREMENT=1013 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=FIXED;
INSERT INTO `contratos` VALUES (38,7,'Telefonica','CAOL','2º Planta',NULL,NULL),(193,11,'Vodafone','Atencion al Cliente','Planta Baja / 1ª Planta',NULL,3);
DROP TABLE IF EXISTS `empresa`;
CREATE TABLE `empresa` (
`Id_empresa` int(10) NOT NULL AUTO_INCREMENT,
`cif` char(10) COLLATE latin1_spanish_ci DEFAULT NULL,
`Razon_Social` char(60) COLLATE latin1_spanish_ci DEFAULT NULL,
`empresa` char(40) COLLATE latin1_spanish_ci DEFAULT NULL,
`Centro` char(40) COLLATE latin1_spanish_ci DEFAULT NULL,
`Provincia` char(30) COLLATE latin1_spanish_ci DEFAULT NULL,
`localizacion_sindicato` char(100) COLLATE latin1_spanish_ci DEFAULT NULL,
`id_setting` int(10) DEFAULT NULL,
`direccion` char(100) COLLATE latin1_spanish_ci DEFAULT NULL,
`cp` char(5) COLLATE latin1_spanish_ci DEFAULT NULL,
`ciudad` char(30) COLLATE latin1_spanish_ci DEFAULT NULL,
`id_cuenta_add` int(10) unsigned DEFAULT '0',
`fecha_baja` datetime DEFAULT NULL,
`id_cuenta_baja` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`Id_empresa`),
KEY `cif` (`cif`),
KEY `empresa` (`empresa`),
KEY `id_setting` (`id_setting`)
) ENGINE=MyISAM AUTO_INCREMENT=871 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=FIXED;
INSERT INTO `empresa` VALUES (7,'A78751997','ATENTO TELESERVICIOS, S.A.','Atento','Indotorre','Sevilla','2º Planta',13,NULL,NULL,NULL,0,NULL,NULL),(11,'B62916077','KONECTA BTO, S.L.','Konecta BTO','Prado de la Torre','Sevilla','1ª Planta (Pasillo Baño)',19,'Prado de la Torre s/n','41110','Bollulos',0,NULL,NULL);
DROP TABLE IF EXISTS `historico`;
CREATE TABLE `historico` (
`Id_historico` int(10) NOT NULL AUTO_INCREMENT,
`id_afiliado` int(10) DEFAULT NULL,
`fecha_alta` datetime DEFAULT NULL,
`tipo_contrato` smallint(5) DEFAULT NULL COMMENT 'Obra y Servicio ,etc ....',
`fecha_baja` datetime DEFAULT NULL,
`motivo_baja` smallint(5) DEFAULT NULL,
`fecha_provision` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`id_contrato` int(10) DEFAULT NULL,
`comentario` char(255) COLLATE latin1_spanish_ci DEFAULT NULL,
`id_cuenta` int(10) DEFAULT NULL,
`Fecha_mod` datetime DEFAULT NULL,
`mod_id_cuenta` int(10) DEFAULT NULL,
PRIMARY KEY (`Id_historico`),
KEY `id_afiliado` (`id_afiliado`),
KEY `id_contrato` (`id_contrato`),
KEY `id_cuenta` (`id_cuenta`),
KEY `tipo_contrato` (`tipo_contrato`)
) ENGINE=MyISAM AUTO_INCREMENT=4814 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci ROW_FORMAT=FIXED;
INSERT INTO `historico` VALUES (227,194,'2005-04-04 00:00:00',0,'2014-01-01 00:00:00',-1,'2012-08-08 10:35:47',38,'',16,NULL,NULL),(1586,194,'2014-01-01 00:00:00',0,NULL,NULL,'2015-08-24 21:14:41',193,'',115,NULL,NULL);
VIEWs:
DROP VIEW IF EXISTS `detalle_full_ko`;
CREATE VIEW `detalle_full_ko` AS
select `afiliados`.`id_afiliado` AS `Id_afiliado`,`afiliados`.`ncarnet` AS `ncarnet`,
`afiliados`.`Id_delegado` AS `Id_delegado`,`afiliados`.`Fecha_Alta` AS `Fecha_Alta_Afiliado`,
`afiliados`.`id_trabajador` AS `id_trabajador`,`afiliados`.`persona` AS `persona`,
`afiliados`.`Nombre` AS `Nombre`,`afiliados`.`Apellidos` AS `Apellidos`,
`afiliados`.`id_fiscal` AS `id_fiscal`,`afiliados`.`Email` AS `Email`,
`afiliados`.`Email_secundario` AS `Email_secundario`,
`afiliados`.`noemail` AS `noemail`,`afiliados`.`noemail_public` AS `noemail_public`,
`afiliados`.`Telefono Principal` AS `Telefono Principal`,
`afiliados`.`Telefono Secundario` AS `Telefono Secundario`,
`afiliados`.`Fax` AS `Fax`,`afiliados`.`Ciudad` AS `Ciudad`,
`afiliados`.`CP` AS `CP`,`afiliados`.`Provincia` AS `Provincia_Afiliado`,
`afiliados`.`fecha_nacimiento` AS `fecha_nacimiento`,
`afiliados`.`Estado` AS `Estado`,`afiliados`.`Es_Delegado` AS `Es_Delegado`,
`afiliados`.`Eliminado` AS `Eliminado`,`detalle_historico_ko`.`fecha_alta` AS `Fecha_Alta_Empresa`,
`empresa`.`Razon_Social` AS `Razon_Social`,`empresa`.`empresa` AS `nombre_empresa`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Departamento` AS `Departamento`,
`contratos`.`Marca` AS `Marca`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`localizacion_sindicato` AS `localizacion_sindicato`,
`detalle_historico_ko`.`Id_contrato` AS `id_contrato`,
`contratos`.`id_empresa` AS `id_empresa`,`detalle_historico_ko`.`tipo_contrato` AS `tipo_contrato`,
`afiliados`.`Fecha_Alta_Sindicato` AS `Fecha_Alta_Sindicato`,
`afiliados`.`Fecha_Baja_Sindicato` AS `Fecha_Baja_Sindicato`,
`afiliados`.`fecha_sync` AS `fecha_sync`,`afiliados`.`noti_alta` AS `noti_alta`,
`afiliados`.`noti_fact` AS `noti_fact`,`afiliados`.`noti_baja` AS `noti_baja`,
`afiliados`.`Fecha_mod` AS `Fecha_mod`,`afiliados`.`LOPD` AS `LOPD`
from (((`contratos`
join `empresa` on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`)))
join `detalle_historico_ko` on(((`empresa`.`Id_empresa` = `detalle_historico_ko`.`Id_empresa`)
and (`contratos`.`Id_contrato` = `detalle_historico_ko`.`Id_contrato`))))
join `afiliados` on((`detalle_historico_ko`.`id_afiliado` = `afiliados`.`id_afiliado`))
)
where (`afiliados`.`LOPD` = 0)
group by `afiliados`.`id_afiliado`;
DROP VIEW IF EXISTS `detalle_full_ok`;
CREATE VIEW `detalle_full_ok` AS
select `afiliados`.`id_afiliado` AS `Id_afiliado`,`afiliados`.`ncarnet` AS `ncarnet`,
`afiliados`.`Id_delegado` AS `Id_delegado`,`afiliados`.`Fecha_Alta` AS `Fecha_Alta_Afiliado`,
`afiliados`.`id_trabajador` AS `id_trabajador`,`afiliados`.`persona` AS `persona`,
`afiliados`.`Nombre` AS `Nombre`,`afiliados`.`Apellidos` AS `Apellidos`,
`afiliados`.`id_fiscal` AS `id_fiscal`,`afiliados`.`Email` AS `Email`,
`afiliados`.`Email_secundario` AS `Email_secundario`,
`afiliados`.`noemail` AS `noemail`,`afiliados`.`noemail_public` AS `noemail_public`,
`afiliados`.`Telefono Principal` AS `Telefono Principal`,
`afiliados`.`Telefono Secundario` AS `Telefono Secundario`,
`afiliados`.`Fax` AS `Fax`,`afiliados`.`Ciudad` AS `Ciudad`,
`afiliados`.`CP` AS `CP`,`afiliados`.`Provincia` AS `Provincia_Afiliado`,
`afiliados`.`fecha_nacimiento` AS `fecha_nacimiento`,
`afiliados`.`Estado` AS `Estado`,`afiliados`.`Es_Delegado` AS `Es_Delegado`,
`afiliados`.`Eliminado` AS `Eliminado`,`detalle_historico_ok`.`fecha_alta` AS `Fecha_Alta_Empresa`,
`empresa`.`Razon_Social` AS `Razon_Social`,`empresa`.`empresa` AS `nombre_empresa`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Departamento` AS `Departamento`,
`contratos`.`Marca` AS `Marca`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`localizacion_sindicato` AS `localizacion_sindicato`,
`detalle_historico_ok`.`Id_contrato` AS `id_contrato`,
`contratos`.`id_empresa` AS `id_empresa`,`detalle_historico_ok`.`tipo_contrato` AS `tipo_contrato`,
`afiliados`.`Fecha_Alta_Sindicato` AS `Fecha_Alta_Sindicato`,
`afiliados`.`Fecha_Baja_Sindicato` AS `Fecha_Baja_Sindicato`,
`afiliados`.`fecha_sync` AS `fecha_sync`,`afiliados`.`noti_alta` AS `noti_alta`,
`afiliados`.`noti_fact` AS `noti_fact`,`afiliados`.`noti_baja` AS `noti_baja`,
`afiliados`.`Fecha_mod` AS `Fecha_mod`,`afiliados`.`LOPD` AS `LOPD`
from (((`contratos`
join `empresa` on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`)))
join `detalle_historico_ok` on(((`empresa`.`Id_empresa` = `detalle_historico_ok`.`Id_empresa`)
and (`contratos`.`Id_contrato` = `detalle_historico_ok`.`Id_contrato`))))
join `afiliados` on((`detalle_historico_ok`.`id_afiliado` = `afiliados`.`id_afiliado`))
)
where (`afiliados`.`LOPD` = 0)
group by `afiliados`.`id_afiliado`;
DROP VIEW IF EXISTS `detalle_historico_ko`;
CREATE VIEW `detalle_historico_ko` AS
select high_priority `historico`.`Id_historico` AS `Id_historico`,
`historico`.`id_afiliado` AS `id_afiliado`,`contratos`.`Id_contrato` AS `Id_contrato`,
`historico`.`fecha_provision` AS `fecha_provision`,`empresa`.`Id_empresa` AS `Id_empresa`,
`empresa`.`empresa` AS `Empresa`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Marca` AS `Marca`,
`contratos`.`Departamento` AS `Departamento`,`historico`.`fecha_alta` AS `fecha_alta`,
`historico`.`fecha_baja` AS `fecha_baja`,`historico`.`comentario` AS `comentario`,
`historico`.`tipo_contrato` AS `tipo_contrato`
from (`empresa`
join (`contratos`
join `historico` on((`contratos`.`Id_contrato` = `historico`.`id_contrato`))) on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`))
)
group by `historico`.`id_afiliado`
order by isnull(`historico`.`fecha_baja`) desc,`historico`.`fecha_baja` desc;
DROP VIEW IF EXISTS `detalle_historico_ok`;
CREATE VIEW `detalle_historico_ok` AS
select high_priority `historico`.`Id_historico` AS `Id_historico`,
`historico`.`id_afiliado` AS `id_afiliado`,`contratos`.`Id_contrato` AS `Id_contrato`,
`historico`.`fecha_provision` AS `fecha_provision`,`empresa`.`Id_empresa` AS `Id_empresa`,
`empresa`.`empresa` AS `Empresa`,`empresa`.`Provincia` AS `Provincia`,
`empresa`.`Centro` AS `Centro`,`contratos`.`Marca` AS `Marca`,
`contratos`.`Departamento` AS `Departamento`,`historico`.`fecha_alta` AS `fecha_alta`,
`historico`.`fecha_baja` AS `fecha_baja`,`historico`.`comentario` AS `comentario`,
`historico`.`tipo_contrato` AS `tipo_contrato`
from (`empresa`
join (`contratos`
join `historico` on((`contratos`.`Id_contrato` = `historico`.`id_contrato`))) on((`empresa`.`Id_empresa` = `contratos`.`id_empresa`))
)
group by `historico`.`Id_historico`,`historico`.`id_afiliado`
order by isnull(`historico`.`fecha_baja`) desc,`historico`.`fecha_baja` desc;
When SELECT the view detalle_full_ok all go right, but if SELECT the view detalle_full_ko go wrong.
View view detalle_full_ok is slow and view detalle_full_ko is fast. The problem is view detalle_full_ko retrieve wrong nombre_empresa field.
Rows:
afiliados(4700)
empresa(800)
contratos(4700)
First pare down the query to get only historico.Id_historico and historico.id_afiliado. That is, avoid any columns and tables you don't need in getting just the desired id pair:
SELECT DISTINCT Id_historico, id_afiliado
FROM ...
-- no GROUP BY or ORDER BY
Then use that as subquery to get the rest of the data:
SELECT lots-of-stuff
FROM ( the above select )
JOIN back to all the tables
WHERE ...
GROUP BY -- probably needed again?
ORDER BY ...
However, the GROUP BY does not include all the non-aggregate values in the SELECT, so it is probably 'improper' and will probably be flagged as an error with ONLY_FULL_GROUP_BY.

MySQL performance, should this take hours?

I am running the following query in MySQL Workbench:
UPDATE directorylistings INNER JOIN postcodelatlng ON PostalCode = postcode
set directoryListings.Latitude = postcodelatlng.latitude, directoryListings.Longitude = postcodelatlng.longitude
WHERE PostalCode = postcode;
Table directorylistings has 9000 records, postcodelatlng has nearly 2 million records.
directorylistings has a postcode (zip code) field and I'm looking this up in postcodelatlng to retrieve and update the latitude and longitude fields in directorylistings.
Ater timeout issues I found I should change DBMS connection read time out, I set this to 36000 (10 hours). The query has been running of 5 hours so far.
My question is, is this time reasonable? Should I be indexing something? Could the query be optimised?
I did make a 2 record version of the postcodelatlng and ran the query, 16 rows were correctly changed in 0.04 seconds, so I know the query is at least workable.
I have looked at a number of Q/A's on indexing and optimising, but it's taken me over a day to get this far and if it fails again I would like to know what approach to follow. I can't keep trying things and waiting hours for the failure! I bet this query should really take seconds. I'm using a MacBook Pro with a 2.5GHz i7, 16 GB RAM and ssd drive.
Many Thanks.
cpu is at 99%. I aborted after 12 hours! to run the show create tables:
CREATE TABLE `postcodelatlng` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`postcode` varchar(8) NOT NULL,
`latitude` decimal(18,15) NOT NULL,
`longitude` decimal(18,15) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1734490 DEFAULT CHARSET=latin1
CREATE TABLE `directorylistings` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Active` tinyint(1) DEFAULT NULL,
`Tag11` tinyint(1) DEFAULT NULL,
`Minyan` tinyint(1) DEFAULT NULL,
`Latitude` double DEFAULT NULL,
`Longitude` double DEFAULT NULL,
`AffiliatedDate` double DEFAULT NULL,
`RegisteredDate` datetime DEFAULT NULL,
`AccountID` varchar(10) DEFAULT NULL,
`ShowListing` tinyint(1) DEFAULT NULL,
`Bloguser` tinyint(1) DEFAULT NULL,
`Trusted` tinyint(1) DEFAULT NULL,
`ClassifiedUser` tinyint(1) DEFAULT NULL,
`HostingID` int(11) DEFAULT NULL,
`EndDate` double DEFAULT NULL,
`EndPromoDate` datetime DEFAULT NULL,
`BaseID` int(11) DEFAULT NULL,
`Banner` varchar(50) DEFAULT NULL,
`PrivateListing` tinyint(1) DEFAULT NULL,
`Reserved1` tinyint(1) DEFAULT NULL,
`Reserved2` tinyint(1) DEFAULT NULL,
`Reserved3` tinyint(1) DEFAULT NULL,
`Reserved4` tinyint(1) DEFAULT NULL,
`Reserved5` tinyint(1) DEFAULT NULL,
`CommunityOrg` tinyint(1) DEFAULT NULL,
`Tag1` tinyint(1) DEFAULT NULL,
`Tag2` tinyint(1) DEFAULT NULL,
`Tag3` tinyint(1) DEFAULT NULL,
`Tag4` tinyint(1) DEFAULT NULL,
`Tag5` tinyint(1) DEFAULT NULL,
`Tag6` tinyint(1) DEFAULT NULL,
`Tag7` tinyint(1) DEFAULT NULL,
`Tag8` tinyint(1) DEFAULT NULL,
`Tag9` tinyint(1) DEFAULT NULL,
`Tag10` tinyint(1) DEFAULT NULL,
`Tag12` tinyint(1) DEFAULT NULL,
`Tag13` tinyint(1) DEFAULT NULL,
`Tag14` tinyint(1) DEFAULT NULL,
`ShomerShabbos` tinyint(1) DEFAULT NULL,
`CategoryID1` int(11) DEFAULT NULL,
`CategoryID2` int(11) DEFAULT NULL,
`CategoryID3` int(11) DEFAULT NULL,
`CategoryID4` int(11) DEFAULT NULL,
`CategoryID5` int(11) DEFAULT NULL,
`CategoryID6` int(11) DEFAULT NULL,
`HiddenCategory` int(11) DEFAULT NULL,
`Company` varchar(100) DEFAULT NULL,
`Slogan` varchar(250) DEFAULT NULL,
`Description` varchar(2000) DEFAULT NULL,
`CharShown` int(11) DEFAULT NULL,
`Address` varchar(100) DEFAULT NULL,
`Unit` varchar(50) DEFAULT NULL,
`Address2` varchar(150) DEFAULT NULL,
`Streetno` varchar(50) DEFAULT NULL,
`Building` varchar(50) DEFAULT NULL,
`Parade` varchar(50) DEFAULT NULL,
`Locality` varchar(50) DEFAULT NULL,
`City` varchar(50) DEFAULT NULL,
`Province` varchar(50) DEFAULT NULL,
`PostalCode` varchar(10) DEFAULT NULL,
`Country` varchar(25) DEFAULT NULL,
`PhoneNumber` varchar(70) DEFAULT NULL,
`Ext` varchar(25) DEFAULT NULL,
`PhoneNumber2` varchar(70) DEFAULT NULL,
`FaxNumber` varchar(30) DEFAULT NULL,
`CellNumber` varchar(30) DEFAULT NULL,
`WebUrl` varchar(100) DEFAULT NULL,
`Email` varchar(75) DEFAULT NULL,
`Password` varchar(30) DEFAULT NULL,
`ContactName` varchar(100) DEFAULT NULL,
`Keywords` varchar(355) DEFAULT NULL,
`Dcount` int(11) DEFAULT NULL,
`LocationID` int(11) DEFAULT NULL,
`Comments` varchar(1500) DEFAULT NULL,
`ApplicantName` varchar(200) DEFAULT NULL,
`ApplicantPhone` varchar(200) DEFAULT NULL,
`ApplicantEmail` varchar(200) DEFAULT NULL,
`Photo1` varchar(250) DEFAULT NULL,
`pRating` int(11) DEFAULT NULL,
`sRating` int(11) DEFAULT NULL,
`vRating` int(11) DEFAULT NULL,
`Private` tinyint(1) NOT NULL DEFAULT '0',
`VeryPrivate` tinyint(1) NOT NULL DEFAULT '0',
`SemiSecure` tinyint(1) NOT NULL DEFAULT '0',
`Secure` tinyint(1) NOT NULL DEFAULT '0',
`Secret` tinyint(1) NOT NULL DEFAULT '0',
`TrustHigh` tinyint(1) NOT NULL DEFAULT '0',
`FL` tinyint(1) NOT NULL DEFAULT '1',
`GJ` tinyint(1) NOT NULL DEFAULT '1',
`Recommended` tinyint(1) NOT NULL DEFAULT '0',
`NonGeo` tinyint(1) NOT NULL DEFAULT '0',
`Edited` datetime DEFAULT NULL,
`Editor` varchar(40) DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID_UNIQUE` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=27167 DEFAULT CHARSET=utf8

SQL Error After Table Import/Export

Hey I am trying to export the product tables from one opencart store to another,but I have had some issues doing that.
The majority of tables had 0 problems to import/export,however the product table gives the following error.
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 37
Here is the code itself.
CREATE TABLE `product` (
`product_id` int(11) NOT NULL,
`model` varchar(64) NOT NULL,
`sku` varchar(64) NOT NULL,
`upc` varchar(12) NOT NULL,
`ean` varchar(14) NOT NULL,
`jan` varchar(13) NOT NULL,
`isbn` varchar(13) NOT NULL,
`mpn` varchar(64) NOT NULL,
`location` varchar(128) NOT NULL,
`quantity` int(4) NOT NULL,
`stock_status_id` int(11) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`manufacturer_id` int(11) NOT NULL,
`shipping` tinyint(1) NOT NULL DEFAULT '1',
`price` decimal(15,4) NOT NULL DEFAULT '0.0000',
`points` int(8) NOT NULL,
`tax_class_id` int(11) NOT NULL,
`date_available` date NOT NULL,
`weight` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`weight_class_id` int(11) NOT NULL,
`length` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`width` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`height` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`length_class_id` int(11) NOT NULL,
`subtract` tinyint(1) NOT NULL DEFAULT '1',
`minimum` int(11) NOT NULL DEFAULT '1',
`sort_order` int(11) NOT NULL,
`status` tinyint(1) NOT NULL,
`date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`viewed` int(5) NOT NULL,
`measurement_class_id` int(11) NOT NULL DEFAULT '0',
`maximum` int(11) NOT NULL DEFAULT '0',
`cost` decimal(15,4) NOT NULL DEFAULT '0.0000',
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0'COMMENT
) ;
Hi you are getting error because you have not completed last line comment code for "style" column do one of the below options:
a) Add comment description
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0' COMMENT "comment description"
b)remove comment description
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0'
remove the COMMENT at the last line:
CREATE TABLE `product` (
`product_id` int(11) NOT NULL,
`model` varchar(64) NOT NULL,
`sku` varchar(64) NOT NULL,
`upc` varchar(12) NOT NULL,
`ean` varchar(14) NOT NULL,
`jan` varchar(13) NOT NULL,
`isbn` varchar(13) NOT NULL,
`mpn` varchar(64) NOT NULL,
`location` varchar(128) NOT NULL,
`quantity` int(4) NOT NULL,
`stock_status_id` int(11) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`manufacturer_id` int(11) NOT NULL,
`shipping` tinyint(1) NOT NULL DEFAULT '1',
`price` decimal(15,4) NOT NULL DEFAULT '0.0000',
`points` int(8) NOT NULL,
`tax_class_id` int(11) NOT NULL,
`date_available` date NOT NULL,
`weight` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`weight_class_id` int(11) NOT NULL,
`length` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`width` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`height` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`length_class_id` int(11) NOT NULL,
`subtract` tinyint(1) NOT NULL DEFAULT '1',
`minimum` int(11) NOT NULL DEFAULT '1',
`sort_order` int(11) NOT NULL,
`status` tinyint(1) NOT NULL,
`date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`viewed` int(5) NOT NULL,
`measurement_class_id` int(11) NOT NULL DEFAULT '0',
`maximum` int(11) NOT NULL DEFAULT '0',
`cost` decimal(15,4) NOT NULL DEFAULT '0.0000',
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0'
) ;
sample
MariaDB []> CREATE TABLE `product` ( `product_id` int(11) NOT NULL, `model` varchar(64) NOT NULL, `sku` varchar(64) NOT NULL, `upc` varchar(12) NOT NULL, `ean` varchar(14) NOT NULL, `jan` varchar(13) NOT NULL, `isbn` varchar(13) NOT NULL, `mpn` varchar(64) NOT NULL, `location` varchar(128) NOT NULL, `quantity` int(4) NOT NULL, `stock_status_id` int(11) NOT NULL, `image` varchar(255) DEFAULT NULL, `manufacturer_id` int(11) NOT NULL, `shipping` tinyint(1) NOT NULL DEFAULT '1', `price` decimal(15,4) NOT NULL DEFAULT '0.0000', `points` int(8) NOT NULL, `tax_class_id` int(11) NOT NULL, `date_available` date NOT NULL, `weight` decimal(15,8) NOT NULL DEFAULT '0.00000000', `weight_class_id` int(11) NOT NULL, `length` decimal(15,8) NOT NULL DEFAULT '0.00000000', `width` decimal(15,8) NOT NULL DEFAULT '0.00000000', `height` decimal(15,8) NOT NULL DEFAULT '0.00000000', `length_class_id` int(11) NOT NULL, `subtract` tinyint(1) NOT NULL DEFAULT '1', `minimum` int(11) NOT NULL DEFAULT '1', `sort_order` int(11) NOT NULL, `status` tinyint(1) NOT NULL, `date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `viewed` int(5) NOT NULL, `measurement_class_id` int(11) NOT NULL DEFAULT '0', `maximum` int(11) NOT NULL DEFAULT '0', `cost` decimal(15,4) NOT NULL DEFAULT '0.0000', `style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0' );
Query OK, 0 rows affected (0.32 sec)
MariaDB []> select version();
+---------------------+
| version() |
+---------------------+
| 10.1.10-MariaDB-log |
+---------------------+
1 row in set (0.00 sec)
MariaDB []>

mysql query can run but some row is missing

Select sale.preselRef As ref,
sale.name,
sale.saledate,
sale.grandtotal,
sale.secid,
sale.invoicetype As typ,
Sum(sellnote.Paid) As cash,
Sum(sellnote.CreCard) As card,
Sum(sellnote.PCheck) As cheque,
Sum(sellnote.Compens) As compen,
sale.grandtotal - Sum(sellnote.Paid) - Sum(sellnote.CreCard) -
Sum(sellnote.PCheck) - Sum(sellnote.Compens) As balanc
From sale, sellnote
Where sale.preselRef = sellnote.Code
Group By sellnote.Code
Having sale.grandtotal - Sum(sellnote.Paid) - Sum(sellnote.CreCard) -
Sum(sellnote.PCheck) - Sum(sellnote.Compens) <> 0
CREATE TABLE IF NOT EXISTS `sale` (
`preselRef` int(12) NOT NULL,
`nameid` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`cusaddress` varchar(255) DEFAULT NULL,
`saledate` date DEFAULT NULL,
`datesent` date DEFAULT NULL,
`vat` decimal(4,2) DEFAULT NULL,
`total` decimal(10,2) DEFAULT NULL,
`vatinbill` decimal(10,2) DEFAULT NULL,
`grandtotal` decimal(10,2) DEFAULT NULL,
`distcount` decimal(10,2) NOT NULL DEFAULT '0.00',
`secid` int(11) DEFAULT NULL,
`invoicetype` enum('d','r','u','i','z') NOT NULL DEFAULT 'u',
`remark` varchar(255) DEFAULT NULL,
`paid` decimal(10,2) DEFAULT NULL,
`cardno` varchar(200) DEFAULT NULL,
`creditcardamount` decimal(10,2) DEFAULT NULL,
`Bankcard` varchar(200) DEFAULT NULL,
`cheqno` varchar(200) DEFAULT NULL,
`chequeamount` decimal(11,2) DEFAULT NULL,
`bankcheque` varchar(200) DEFAULT NULL,
`asgreeno` varchar(200) DEFAULT NULL,
`checkcleardate` date DEFAULT NULL,
`Checktype` enum('r','s') NOT NULL,
`datetime` datetime DEFAULT NULL,
`timestamp` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`preselRef`),
UNIQUE KEY `salein` (`preselRef`),
UNIQUE KEY `saleid` (`secid`,`invoicetype`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `sellnote` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Code` varchar(11) NOT NULL DEFAULT '',
`Pdtail` varchar(200) DEFAULT NULL,
`Paid` decimal(12,2) NOT NULL DEFAULT '0.00',
`CreCard` decimal(12,2) NOT NULL DEFAULT '0.00',
`PCheck` decimal(12,2) NOT NULL DEFAULT '0.00',
`Compens` decimal(12,2) NOT NULL DEFAULT '0.00',
`DateDue` date DEFAULT NULL,
`BillCan` enum('Y','N') NOT NULL DEFAULT 'N',
`Remark` varchar(150) DEFAULT NULL,
`staff` varchar(50) DEFAULT NULL,
`Timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`),
KEY `Code` (`Code`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Transaction Header' AUTO_INCREMENT=4943 ;
I have one row intable sale
4058 613 บริษัท เดอ ละไม จำกัด 171/4-9 หมู่4 ต.มะเร็ต อ.เกาะสมุย
จ.สุราษฎร์ธานี 2011-08-01 NULL 7.00 3271.03 228.97 3500.00 0.00 10069 i NULL 0.00 0.00 0.00 NULL NULL NULL
and do not have sellnote.Code = 4058 but the row of the table sale that have sale.preselRef it should show but it do not show I have a bout 5000 record and only this row that i found the problem plese help