Migrating an .sql database to WordPress - mysql

I have an SQL file for a database from an old custom blog CMS. I am trying to import the articles into WordPress but I am now stuck. So far, I have managed to isolate the table structures for the CMS (see below). I have also identified the columns that i need. These are (Post, Title, Date, Image, Category). Now I have to map these columns to some columns in the Wordpress schema somehow and that is where I am getting stuck.
Here is the SQL structure of the database:
CREATE TABLE `ads_ng_ads` (
`id` int(10) unsigned NOT NULL auto_increment,
`ad_type` enum('media','text','html') character set latin1 collate latin1_bin NOT NULL,
`media_type` enum('image','flash','other') character set latin1 collate latin1_bin default NULL,
`media_width` smallint(6) default NULL,
`media_height` smallint(6) default NULL,
`media_size` int(6) default NULL,
`media_path` varchar(128) character set latin1 collate latin1_bin default NULL,
`uri` varchar(128) character set utf8 collate utf8_bin NOT NULL,
`is_hidden` tinyint(1) NOT NULL,
`created_on` int(11) NOT NULL,
`active_from` int(11) NOT NULL,
`active_to` int(11) NOT NULL,
`room_id` int(11) default NULL,
`geo_target` int(11) default NULL,
`alternative` text character set utf8 NOT NULL,
`zone_target` text character set ascii collate ascii_bin NOT NULL,
`uri_target` text character set utf8 collate utf8_bin NOT NULL,
PRIMARY KEY (`id`),
KEY `zone_target` (`is_hidden`,`active_from`,`active_to`,`geo_target`)
) ENGINE=MyISAM AUTO_INCREMENT=757 DEFAULT CHARSET=latin1;
CREATE TABLE `ads_ng_statistics` (
`ad_id` int(10) unsigned NOT NULL,
`date` int(11) NOT NULL,
`views` mediumint(8) unsigned NOT NULL default '0',
`clicks` mediumint(8) unsigned NOT NULL default '0',
PRIMARY KEY (`ad_id`,`date`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `countries` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
`alias` varchar(3) NOT NULL,
`restricted` tinyint(3) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=253 DEFAULT CHARSET=utf8;
CREATE TABLE `featured_column` (
`id` int(11) unsigned NOT NULL auto_increment,
`date_modified` timestamp NOT NULL default CURRENT_TIMESTAMP,
`date_created` timestamp NOT NULL default '1000-01-01 00:00:00',
`title` varchar(255) NOT NULL,
`alias` varchar(255) NOT NULL,
`body` text NOT NULL,
`body_html` text NOT NULL,
`user_id` int(11) NOT NULL,
`tags` varchar(255) NOT NULL,
`summary` varchar(255) NOT NULL,
`in_homepage` int(1) NOT NULL default '0',
`img_title` int(1) NOT NULL default '0',
`meta_t` varchar(255) NOT NULL,
`meta_k` varchar(255) NOT NULL,
`meta_d` varchar(255) NOT NULL,
`public` int(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1381 DEFAULT CHARSET=utf8;

Assuming that you're using the latest WordPress version(4.9.1), you just need to create a script that will import all data to table wp_posts for example if you want to import as WordPress posts. Example: "Title" to "post_title", "post" to "post_content", etc.

Related

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.

Laravel where method won't works correctly on my server but work right on my Mac

I have a weird problem, this line wont fetch any record on my server but works all right on my computer, mysql and php version are same, and when i cast id field to string it's work on server (Debian), but it is risky because i use where statement with id very much. this is the code:
$myAnswers=Questioner::where('id',$questioner_id)->first()->getLinkedAnsweres()->where("subject_id",Auth::user()->id);
and when i change it to this, it's work right (cast id to string):
$myAnswers=Questioner::where('id',$questioner_id)->first()->getLinkedAnsweres()->where("subject_id",(string)Auth::user()->id);
subject_id and user id are both unsigned_integer with same size.
How can i fix this problem? Should i overwrite where method? or is there any config which cast unsigned int to string in query?
thanks
PS: Tables structure:
users | CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`family` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`gender` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`default_password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`access_level` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'subject',
`phone` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`comments` longtext COLLATE utf8_unicode_ci,
`project_limit` int(10) unsigned NOT NULL,
`questioner_limit` int(10) unsigned NOT NULL,
`created_by` int(10) unsigned DEFAULT NULL,
`remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_username_unique` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
answers | CREATE TABLE `answers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`question_id` int(10) unsigned NOT NULL,
`selected_option` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`subject_id` int(10) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |

query with multiple joins and indexes stuck on "Copying to tmp table"

I have this query that doesn't run under odd circumstances. It's doing two joins, I have two indexes of the same name on two different tables (but this shouldn't matter). When I remove one of the indexes from either tables, the query runs fine. Something else that makes it run is removing the order by clause, but I definitely need that. When I run show processlist the state is stuck on "Copying to tmp table". The indexes I'm talking about are channelID.
the query
SELECT twitterTweets.*,
COUNT(twitterRetweets.id) AS retweets,
sTwitter.followers,
sTwitter.date
FROM twitterTweets
LEFT JOIN twitterRetweets
ON twitterTweets.id = twitterTweetsID
JOIN sTwitter
ON DATE(twitterTweets.dateCreated) = sTwitter.date
AND twitterTweets.channelID = sTwitter.channelID
WHERE twitterTweets.channelID = 32
AND type = 'tweet'
AND DATE(dateCreated) >= '2013-12-05'
AND DATE(dateCreated) <= '2014-01-05'
GROUP BY twitterTweets.id
ORDER BY dateCreated
explain results
1 SIMPLE sTwitter ref channelID channelID 5 const 1162 Using where; Using temporary; Using filesort
1 SIMPLE twitterTweets ref channelID channelID 5 const 17456 Using where
1 SIMPLE twitterRetweets ref twitterTweetsID twitterTweetsID 5 social.twitterTweets.id 3
create for the three tables
CREATE TABLE `twitterTweets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientID` int(11) DEFAULT NULL,
`divisionID` int(11) DEFAULT NULL,
`accountID` int(11) DEFAULT NULL,
`channelID` int(11) DEFAULT NULL,
`type` enum('tweet','reply','direct in','direct out') COLLATE utf8_unicode_ci DEFAULT 'tweet',
`subType` enum('reply beginning','retweet beginning','reply middle','retweet middle') COLLATE utf8_unicode_ci DEFAULT NULL,
`tweetID` bigint(20) DEFAULT NULL,
`tweet` text COLLATE utf8_unicode_ci,
`replies` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`hash` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`retweet` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`source` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`media` text COLLATE utf8_unicode_ci,
`tweetUrls` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`expandedUrls` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`replyToStatus` bigint(20) DEFAULT NULL,
`user` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`followers` int(11) DEFAULT NULL,
`following` int(11) DEFAULT NULL,
`updates` int(11) DEFAULT NULL,
`group1` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`campaign` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`segment` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`destination` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`retweets` smallint(6) DEFAULT '0',
`favorites` smallint(6) DEFAULT NULL,
`dateCreated` datetime DEFAULT NULL,
`dateAdded` datetime DEFAULT NULL,
`dateUpdated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `channelID` (`channelID`)
) ENGINE=MyISAM AUTO_INCREMENT=296264 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `twitterRetweets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientID` int(11) DEFAULT NULL,
`divisionID` int(11) DEFAULT NULL,
`accountID` int(11) DEFAULT NULL,
`channelID` int(11) DEFAULT NULL,
`twitterTweetsID` int(11) DEFAULT NULL,
`tweetID` bigint(20) DEFAULT NULL,
`screenName` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`followers` int(11) DEFAULT NULL,
`following` int(11) DEFAULT NULL,
`updates` int(11) DEFAULT NULL,
`favorites` smallint(6) DEFAULT NULL,
`dateAdded` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `twitterTweetsID` (`twitterTweetsID`)
) ENGINE=MyISAM AUTO_INCREMENT=93821 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `sTwitter` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientID` int(11) DEFAULT NULL,
`divisionID` int(11) DEFAULT NULL,
`accountID` int(11) DEFAULT NULL,
`channelID` int(11) DEFAULT NULL,
`following` int(11) DEFAULT '0',
`followers` int(11) DEFAULT '0',
`updates` int(11) DEFAULT '0',
`date` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `channelID` (`channelID`)
) ENGINE=MyISAM AUTO_INCREMENT=35615 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
So how do I make this thing run with both indexes in place? Do I have to ignore one of them?

Synchronize Table data with a whole different structure

The goal is to synchronize the customers data and his table structure to our table structure.
The table structure is the following:
CREATE TABLE IF NOT EXISTS `BildSerie_W2L` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`BildserieID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Prislista` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`BildKod` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`AvdKlassID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Status` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=24 ;
CREATE TABLE IF NOT EXISTS `classes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`client_id` int(10) unsigned NOT NULL,
`class` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`year` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `classes_client_id_foreign` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=9 ;
CREATE TABLE IF NOT EXISTS `Elever_W2L` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`X_ElevID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Efternamn` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Förnamn` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`GataMedNr` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`PostNr` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Postort` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`TelefonMobil` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Epost1` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Epost2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`X_AvdKlassId` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Status` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=18 ;
CREATE TABLE IF NOT EXISTS `Elev_Bild_BildSerie_W2L` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Elev_Bild_Bildserie_ID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`BildSerieID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`ElevID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Status` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=26 ;
CREATE TABLE IF NOT EXISTS `image_codes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`client_id` int(10) unsigned DEFAULT NULL,
`code` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `image_codes_code_unique` (`code`),
KEY `image_codes_client_id_foreign` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=20 ;
CREATE TABLE IF NOT EXISTS `image_series` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`image_code_id` int(10) unsigned NOT NULL,
`pricelist_id` int(10) unsigned NOT NULL DEFAULT '1',
`class_id` int(10) unsigned DEFAULT NULL,
`image_serie` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`year` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `image_series_image_code_id_foreign` (`image_code_id`),
KEY `image_series_pricelist_id_foreign` (`pricelist_id`),
KEY `image_series_class_id_foreign` (`class_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
The goal is to take the data in "BildSerie_W2L" and populate it in to image_series.
My query looks like this:
SELECT `BildSerie_W2L`.*, `classes`.id, `image_codes`.id as ImageID, `Elever_W2L`.Förnamn, `Elever_W2L`.Efternamn
FROM `BildSerie_W2L`
INNER JOIN `image_codes` ON `BildSerie_W2L`.BildKod = `image_codes`.code
INNER JOIN `classes` ON `BildSerie_W2L`.AvdKlassID = `classes`.class
INNER JOIN `Elev_Bild_BildSerie_W2L` ON `Elev_Bild_BildSerie_W2L`.BildSerieID = `BildSerie_W2L`.BildserieID
INNER JOIN `Elever_W2L` ON `Elever_W2L`.X_ElevID = `Elev_Bild_BildSerie_W2L`.ElevID
WHERE `BildSerie_W2L`.Status = 0
However it gives back duplicated results and not all the data in BildSerie_W2L...

bugs in mysql when connecting 2 table

When I am trying to rum query I find error like
#1267 - Illegal mix of collations (latin1_general_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '='
My table structure are
CREATE TABLE `empregistration` (
`id` int(100) NOT NULL auto_increment,
`cname` text NOT NULL,
`cpersonname` text NOT NULL,
`roaddress` text NOT NULL,
`txtcity` text NOT NULL,
`txtstate` text NOT NULL,
`txtcountry` text NOT NULL,
`faxno` varchar(255) NOT NULL,
`contactno` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
`empCat` varchar(255) NOT NULL,
`empno` varchar(255) NOT NULL,
`ctype` varchar(255) NOT NULL,
`establishyear` varchar(255) NOT NULL,
`txtjobcategory` text NOT NULL,
`cemailid` varchar(255) NOT NULL,
`calteremailid` varchar(255) NOT NULL,
`aboutcompany` text NOT NULL,
`password` varchar(255) NOT NULL,
`conpassword` varchar(255) NOT NULL,
`empLogo` varchar(255) NOT NULL,
`paymenttype` varchar(255) NOT NULL,
`regDate` int(11) default NULL,
`countViewProf` varchar(200) default '0',
`cntDownProf` varchar(200) default '0',
`cntJobPost` varchar(200) default '0',
`cntupdatejob` varchar(200) NOT NULL default '0',
`limitjobpost` varchar(200) NOT NULL default '0',
`limitofupdatejob` varchar(200) NOT NULL default '0',
`limitofviewcv` varchar(200) NOT NULL default '0',
`limitofdowncv` varchar(200) NOT NULL default '0',
`paymentforpostjob` varchar(200) NOT NULL default '0',
`paymentforsearchcv` varchar(200) NOT NULL default '0',
`takenPlan` varchar(255) default '0',
`planDate` date default NULL,
`takenplanforpostjob` varchar(200) NOT NULL default '0',
`postjobplandate` varchar(200) NOT NULL default '0',
`cntJobPost1` varchar(200) default '0',
`cntupdatejob1` varchar(200) default '0',
`status` varchar(255) NOT NULL default 'Active',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=214 ;
and other table is
CREATE TABLE `blockedcompanies` (
`blockedId` int(11) NOT NULL auto_increment,
`empId` varchar(50) collate latin1_general_ci NOT NULL,
`regId` int(11) NOT NULL,
`jobSeekId` varchar(50) collate latin1_general_ci NOT NULL,
`blockDate` date NOT NULL,
PRIMARY KEY (`blockedId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=5 ;
You don't show your query, but i expect you're doing a where somehow with an '=' in there. Now your default collation seems to be swedish, and your 'blockedcompanies' table has a collation to latin1_general.
The error says "implicit", so you're not doing something with collation in your query. Now you have to different collation being compared. You've got to change that using COLLATE
check this: http://dev.mysql.com/doc/refman/5.0/en/charset-collate.html
when you create the table empregistration, change the final line from
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=214 ;
to
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=214 ;
and your query should start working.