2014-07-03 104 views
2

我正在將表數據從一個CMS導入到另一個CMS。因此,我有一個帶有「pn_」前綴的模型,它是舊的。新表的前面帶有「avsn_」。奇怪的SQL連接產生的結果不同於分割結果

有遊戲變種,畫廊圖像和畫廊類別。這些表是這樣的:

CREATE TABLE `avsn_gallery_categories2variants` (
    `categoryId` int(255) unsigned NOT NULL AUTO_INCREMENT, 
    `gameVariantId` int(255) NULL, 
    PRIMARY KEY (`categoryId`,`gameVariantId`) 
); 

CREATE TABLE `avsn_games_variant` (
     `psId` int(255) NOT NULL , 
     `variantId` int(255) NOT NULL AUTO_INCREMENT , 
     `gameId` int(255) NOT NULL , 
     `variantTitle` varchar(1000) NOT NULL , 
     `consoleId` int(255) NOT NULL ,  
     `specialGenreID` int(255) NULL, 
     `releaseDay` int(2) NOT NULL , 
     `releaseMonth` int(2) NOT NULL , 
     `releaseYear` int(4) NOT NULL , 
     `isFreeware` bit NOT NULL , 
     `isReleased` bit NOT NULL , 
     `installationAdvices` text NULL , 
     `gameAdvices` text NULL , 
     `additionalInformation` text NULL , 
     `cover` varchar(1000) NOT NULL , 
     `publishingStatus` int(1) NOT NULL , 
     `creatorId` int(255) NOT NULL , 
     `lastModified` date NOT NULL , 
     `creationDate` date NOT NULL , 
     PRIMARY KEY (`variantId`) 
    ); 

舊錶是這些:

CREATE TABLE `pn_gallery_categories` (
    `gallid` int(3) NOT NULL AUTO_INCREMENT, 
    `gallname` varchar(80) NOT NULL DEFAULT '', 
    `gallimg` varchar(50) NOT NULL DEFAULT '', 
    `server` int(11) NOT NULL DEFAULT '1', 
    `galloc` longtext NOT NULL, 
    `description` text NOT NULL, 
    `parent` int(3) NOT NULL DEFAULT '-1', 
    `visible` int(1) NOT NULL DEFAULT '0', 
    `template` varchar(25) NOT NULL DEFAULT 'Default', 
    `thumbwidth` int(2) unsigned NOT NULL DEFAULT '120', 
    `numcol` tinyint(3) unsigned NOT NULL DEFAULT '0', 
    `total` int(10) unsigned NOT NULL DEFAULT '0', 
    `lastadd` date DEFAULT NULL, 
    PRIMARY KEY (`gallid`), 
    KEY `gallid` (`gallid`) 
) ENGINE=MyISAM AUTO_INCREMENT=2166 DEFAULT CHARSET=latin1; 
/*!40101 SET character_set_client = @saved_cs_client */; 

表avsn_gallery_categories2variants不正確導入。一個特定類別與一個變體有多個組合。這應該是不可能的,因爲舊模型只允許一種組合。我覺得有什麼不妥的進口JOIN:

INSERT INTO 
    `avsn_gallery_categories2variants` 
    (
     SELECT 
      variant.variantId AS `variantId`, 
      gallid   AS `categoryId` 
     FROM 
      pn_gallery_categories 
     JOIN 
      avsn_games_variant variant ON gallimg = variant.cover 
    ); 

我所做的驗證:

1確認,真的有1類:

mysql> SELECT * FROM avsn_gallery_categories WHERE categoryId = 1897; 
+------------+----------+---------------+---------------+--------+----------------+ 
| categoryId | parentId | title   | folder  | userId | isUserCategory | 
+------------+----------+---------------+---------------+--------+----------------+ 
|  1897 |  630 | Gray Matter 2 | G/GrayMatter2 |  -1 |    | 
+------------+----------+---------------+---------------+--------+----------------+ 
1 row in set (0.00 sec) 

2進行當然,直接請求它只有一個關聯:

mysql> SELECT * FROM avsn_gallery_categories2variants WHERE categoryId = 1897; 
+------------+---------------+ 
| categoryId | gameVariantId | 
+------------+---------------+ 
| 1897 | 365 | 
| 1897 | 542 | 
+------------+---------------+ 
2 rows in set (0.01 sec) 

糟糕! 2排。好了,現在檢查聯接領域的這兩種方式:

類別的封面:

mysql> SELECT gallimg FROM pn_gallery_categories WHERE gallid = 1897; 
+-----------------+ 
| gallimg   | 
+-----------------+ 
| graymatter2.png | 
+-----------------+ 
1 row in set (0.00 sec) 

ID 365:

mysql> SELECT cover FROM avsn_games_variant WHERE variantId = 365; 
+-----------+ 
| cover  | 
+-----------+ 
| radau.jpg | 
+-----------+ 
1 row in set (0.00 sec) 

ID 542:

mysql> SELECT cover FROM avsn_games_variant WHERE variantId = 542; 
+-----------+ 
| cover  | 
+-----------+ 
| tkkg5.jpg | 
+-----------+ 
1 row in set (0.00 sec) 

ERRM。是啊。它們都不符合該類別。出了什麼問題?我有點絕望地找到解決方案。 Mabye我寫了太多的SQL,並且因爲擁有像5,5k行的導入腳本而無法再看到問題。

我很感激任何好的答案!

回答

1

我覺得應該是:

INSERT INTO 
    `avsn_gallery_categories2variants` 
    (
     SELECT 
      gallid   AS `categoryId`, 
      variant.variantId AS `variantId` 
     FROM 
      pn_gallery_categories 
     JOIN 
      avsn_games_variant variant ON gallimg = variant.cover 
    ); 

或者更好的是:

INSERT INTO 
    `avsn_gallery_categories2variants` 
    (`categoryId`, `variantId`) 
    (
     SELECT 
      gallid, 
      variant.variantId 
     FROM 
      pn_gallery_categories 
     JOIN 
      avsn_games_variant variant ON gallimg = variant.cover 
    ); 

我從the official documentation得到,如果你不明確聲明目標列,列在內部訂單select應該匹配目標表中列的順序。我不認爲在選擇部分對列進行鋸齒是沒有任何作用的。

+0

哦男人....我真是個傻瓜。謝謝你在我臉上的正面衝擊:D就是這樣,謝謝。這是我的愚蠢:D –

+1

不客氣。發生在我們所有人身上:) –