How to compare three table products?

567 views Asked by At

I am working on a price comparison website using 3 API (Linkshare, commission junction and amazon). In the database, Table fields are not relevant with each other. But product names are similar to other tables.

Step 1: I want to merge all three tables and shown in single table. Is this possible?

Step 2: compare the three tables and list out the product in frontend.

note: my primary keys in three tables are: commission junction: SKU Amazon: aid LinkShare:SKUNUMBER

This is my database structure. enter image description here

My table DDL structure for 3API. Amazon table structure

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

Table structure for Commission junction.

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

Table structure for 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

How do I solve this and please provide me a helpful advice.

2

There are 2 answers

6
Viacheslav Dobromyslov On BEST ANSWER

The best way for you is one stored procedure which:

  1. Creates temporary table for comparison:

    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. Fills this table with data from your original 3 tables:

    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;
    

As a result you will get the temporary table with all interesting data. This table is like a cache and should be updated every time you touch any original table.

Then you just select anything you want from comparison_table and join necessary original tables by origin and sku. For example:

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`
3
bonCodigo On

If you don't have any relationship between these table but you have same data columns, then the ugly way to do is ..UNION. As you mentioned product names are a common column...

Select product_name from t1
Union
Select product_name from t2
....