我正在使用3 API(Linkshare,佣金交界处和亚马逊)进行价格比较网站。在数据库中,表字段彼此不相关。但产品名称与其他表类似。
步骤1:我想合并所有三个表并显示在单个表中。这可能吗?
第2步:比较三个表并在前端列出产品。
注意:我在三个表中的主键是: 佣金交汇处:SKU 亚马逊:援助 LinkShare的:SKUNUMBER
这是我的数据库结构。
我的桌面DDL结构为3API。亚马逊表结构
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
linkshare的表结构:
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
我如何解决这个问题,请给我一个有用的建议。
答案 0 :(得分:2)
如果这些表之间没有任何关系但是你有相同的数据列,那么丑陋的方法就是...... UNION
。正如您所提到的,product names
是一个共同的专栏......
Select product_name from t1
Union
Select product_name from t2
....
答案 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`)
);
使用原始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
中选择所需内容,然后按origin
和sku
加入必要的原始表格。例如:
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`