将表数据与完全不同的结构同步

时间:2013-08-01 14:32:31

标签: mysql sql

目标是将客户数据及其表结构与表结构同步。

表结构如下:

    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 ;

目标是获取“BildSerie_W2L”中的数据并将其填充到image_series中。

我的查询如下:

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

然而,它会返回重复的结果,而不是BildSerie_W2L ...

中的所有数据

0 个答案:

没有答案