-1

我正在使用 3 API(Linkshare、commission junction 和 amazon)开发一个价格比较网站。在数据库中,表字段彼此不相关。但产品名称与其他表类似。

第 1 步:我想合并所有三个表并显示在单个表中。这可能吗?

第二步:对比三张表,在前端列出产品。

注意:我在三个表中的主键是:commission junction: SKU Amazon:aid LinkShare:SKUNUMBER

这是我的数据库结构。 在此处输入图像描述

我的 3API 表 DDL 结构。亚马逊表结构

CREATE TABLE `amazon` ( `aid` bigint(20) NOT NULL AUTO_INCREMENT, `network` varchar(230) NOT NULL, `merchant_id` varchar(250) NOT NULL, `merchant_name` varchar(250) NOT NULL, `merchant_logo` mediumtext NOT NULL, `name` varchar(250) NOT NULL, `brand` varchar(250) NOT NULL, `asin` varchar(250) NOT NULL, `upc` varchar(150) NOT NULL, `ean` varchar(300) NOT NULL,
 `image` mediumtext NOT NULL, `description` text NOT NULL, `url` mediumtext NOT NULL, `price` decimal(10,0) NOT NULL, `listprice` decimal(10,0) NOT NULL, `lowest_usedprice` decimal(10,0) NOT NULL, `shipping` varchar(130) NOT NULL, `currency` varchar(5) NOT NULL, `search_keyword` text NOT NULL, `search_maxprice` varchar(230) NOT NULL, `search_minprice` varchar(230) NOT NULL, `date` datetime NOT NULL, `catagory` varchar(255) NOT NULL, PRIMARY KEY (`aid`)) ENGINE=InnoDB AUTO_INCREMENT=68 DEFAULT CHARSET=latin1

委员会结点的表结构。

CREATE TABLE `cjfeeds` ( `PROGRAMNAME` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `PROGRAMURL` varchar(300) COLLATE utf8_unicode_ci DEFAULT NULL, `CATALOGNAME` varchar(130) COLLATE utf8_unicode_ci DEFAULT NULL, `LASTUPDATED` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `NAME` varchar(160) COLLATE utf8_unicode_ci DEFAULT NULL, `KEYWORDS` varchar(300) COLLATE utf8_unicode_ci DEFAULT NULL, `DESCRIPTION` varchar(3000) COLLATE utf8_unicode_ci DEFAULT NULL, `SKU` varchar(100) COLLATE utf8_unicode_ci NOT NULL DEFAULT '', `MANUFACTURER` varchar(160) COLLATE utf8_unicode_ci DEFAULT NULL, `MANUFACTURERID` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, `UPC` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `ISBN` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
 `CURRENCY` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL, `SALEPRICE` decimal(10,2) DEFAULT NULL, `PRICE` decimal(10,2) DEFAULT NULL, `RETAILPRICE` decimal(10,2) DEFAULT NULL,
 `FROMPRICE` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL, `BUYURL` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL, `IMPRESSIONURL` varchar(300) COLLATE utf8_unicode_ci DEFAULT NULL, `IMAGEURL` varchar(300) COLLATE utf8_unicode_ci DEFAULT NULL, `ADVERTISERCATEGORY` varchar(300) COLLATE utf8_unicode_ci DEFAULT NULL, `THIRDPARTYID` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, `THIRDPARTYCATEGORY` varchar(300) COLLATE utf8_unicode_ci DEFAULT NULL, `AUTHOR` varchar(130) COLLATE utf8_unicode_ci DEFAULT NULL, `ARTIST` varchar(130) COLLATE utf8_unicode_ci DEFAULT NULL, `TITLE` varchar(130) COLLATE utf8_unicode_ci DEFAULT NULL, `PUBLISHER` varchar(130) COLLATE utf8_unicode_ci DEFAULT NULL, `LABEL` varchar(130) COLLATE utf8_unicode_ci DEFAULT NULL, `FORMAT` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, `SPECIAL` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL, `GIFT` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL, `PROMOTIONALTEXT` varchar(300) COLLATE utf8_unicode_ci DEFAULT NULL, `STARTDATE` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `ENDDATE` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
 `OFFLINE` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL, `ONLINE` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL, `INSTOCK` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL,
 `CONDITION` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL, `WARRANTY` varchar(300) COLLATE utf8_unicode_ci DEFAULT NULL, `STANDARDSHIPPINGCOST` decimal(10,2) DEFAULT NULL, PRIMARY KEY (`SKU`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

链接共享的表结构:

CREATE TABLE `linkshare` ( `PRODUCTID` int(100) unsigned NOT NULL, `PRODUCTNAME` varchar(255) NOT NULL, `SKUNUMBER` varchar(200) NOT NULL, `PRIMARYCATAGRY` varchar(255) NOT NULL, `SECONDARYCATAGRY` varchar(2000) NOT NULL, `PRODUCTURL` varchar(2000) NOT NULL, `PRODUCTIMAGEURL` varchar(2000) NOT NULL, `BUYURL` varchar(2000) NOT NULL, `SHORTPRODUCTDESCRIPTION` varchar(2000) NOT NULL, `LONGPRODUCTDESCRIPTION` varchar(2000) NOT NULL, `DISCOUNT` float(8,2) unsigned NOT NULL,
 `DISCOUNTTYPE` varchar(255) NOT NULL, `SALEPRICE` float(8,2) unsigned NOT NULL, `RETAILPRICE` float(8,2) unsigned NOT NULL,
 `BEGINDATE` datetime NOT NULL, `ENDDATE` datetime NOT NULL, `BRAND` varchar(255) NOT NULL, `SHIPPING` int(255) unsigned NOT NULL, `KEYWORDS` varchar(500) NOT NULL, `MANUFACTURERPART` varchar(100) NOT NULL, `MANUFACTURERNAME` varchar(250) NOT NULL,
 `SHIPPINGINFORMATION` varchar(50) NOT NULL, `AVAILABLITY` varchar(50) NOT NULL, `UNIVERSALPRODUCTCODE` varchar(15) NOT NULL,
 `CLASSID` float(8,2) unsigned NOT NULL, `CURRENCY` varchar(3) NOT NULL, `M1` varchar(2000) NOT NULL, `PIXEL` varchar(255) NOT NULL, `MISCELLANEOUSATTRIBUTE` varchar(255) NOT NULL, `ATTRIBUTE1` varchar(255) NOT NULL, `ATTRIBUTE2` varchar(255) NOT NULL, `ATTRIBUTE3` varchar(255) NOT NULL, `ATTRIBUTE4` varchar(255) NOT NULL, `ATTRIBUTE5` varchar(255) NOT NULL, ATTRIBUTE6` varchar(255) NOT NULL, `ATTRIBUTE7` varchar(255) NOT NULL, `ATTRIBUTE8` varchar(255) NOT NULL, `ATTRIBUTE9` varchar(255) NOT NULL, `ATTRIBUTE10` varchar(255) NOT NULL, UNIQUE KEY `PRODUCTID` (`PRODUCTID`)) ENGINE=MyISAM DEFAULT CHARSET=utf8

我该如何解决这个问题,请给我一个有用的建议。

4

2 回答 2

2

如果您在这些表之间没有任何关系但您有相同的数据列,那么丑陋的做法是UNION...... 正如您所提到product names的,这是一个常见的列...

Select product_name from t1
Union
Select product_name from t2
....
于 2013-02-02T06:11:32.397 回答
1

对您来说最好的方法是一个存储过程,它:

  1. 创建临时表进行比较:

    DROP TABLE IF EXISTS comparison_table;
    
    CREATE TEMPORARY TABLE `comparison_table` (
        `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT 'Primary key',
        `name` VARCHAR(255) NOT NULL COMMENT 'Product name',
        `origin` VARCHAR(10) NOT NULL COMMENT 'Original website id, i.e. AZ, LS, CJ',
        `sku` VARCHAR(10) NOT NULL COMMENT 'SKU on the original website',
        `price` FLOAT NOT NULL COMMENT 'Price on the original website',
        PRIMARY KEY (`id`)
    );
    
  2. 使用原始 3 个表中的数据填充此表:

    INSERT INTO `comparison_table` (`name`, `origin`, `sku`, `price`)
    SELECT `az`.`name`, 'AZ', `az`.`aid`, `az`.`price`
    FROM `amazon` `az`
    WHERE `az`.`name` IS NOT NULL AND `az`.`aid` IS NOT NULL AND `az`.`price` IS NOT NULL;
    
    INSERT INTO `comparison_table` (`name`, `origin`, `sku`, `price`)
    SELECT `cj`.`name`, 'CJ', `cj`.`sku`, `cj`.`price`
    FROM `cjfeeds` `cj`
    WHERE `cj`.`name` IS NOT NULL AND `cj`.`sku` IS NOT NULL AND `cj`.`price` IS NOT NULL;
    
    INSERT INTO `comparison_table` (`name`, `origin`, `sku`, `price`)
    SELECT `ls`.`productname`, 'LS', `ls`.`skunumber`, `ls`.`retailprice`
    FROM `linkshare` `ls`
    WHERE `ls`.`productname` IS NOT NULL AND `ls`.`skunumber` IS NOT NULL AND `ls`.`retailprice` IS NOT NULL;
    

结果,您将获得包含所有有趣数据的临时表。这个表就像一个缓存,每次你接触任何原始表时都应该更新。

然后,您只需从中选择任何您想要的内容,并通过和comparison_table加入必要的原始表格。例如:originsku

SELECT `ct`.`id`, `ct`.`name`, `ct`.`price`, 
    CASE `ct`.`origin` 
        WHEN 'AZ' THEN `az`.`impressionurl`
        WHEN 'CJ' THEN `cj`.`url`
        WHEN 'LS' THEN `ls`.`producturl`
    END `url`
FROM `comparison_table` `ct`
LEFT JOIN `amazon` `az` ON (`ct`.`origin` = 'AZ' AND `ct`.`sku` = `az`.`aid`)
LEFT JOIN `cjfeeds` `cj` ON (`ct`.`origin` = 'CJ' AND `ct`.`sku` = `cj`.`sku`)
LEFT JOIN `linkshare` `ls` ON (`ct`.`origin` = 'LS' AND `ct`.`sku` = `ls`.`skunumber`)
WHERE `ct`.`name` = 'YOUR PRODUCT NAME'
ORDER BY `ct`.`price`
于 2013-02-02T07:16:34.173 回答