奇怪的SQL连接产生的结果与拆分结果不同

时间:2014-07-03 14:59:19

标签: mysql sql

我正在将表数据从一个CMS导入到另一个CMS。因此,我有" pn _"前缀,这是旧的。新表的表格带有" avsn _"在前面。

有游戏变体,图库图像和图库类别。表格如下所示:

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确保只有一个类别:

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排。好的,现在检查两个变体的JOIN字段:

该类别的封面:

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 个答案:

答案 0 :(得分: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中列的顺序应该与目标表中列的顺序相匹配。我不认为对选择部分中的列进行别名会产生任何影响。