dimanche 23 juillet 2017

Get the most product from 3 table

i have a case here, i'd like to create a top 10 product based on 3 table, which is the most of views, emails_request, and apply_request, my table structure is like this

product_views

===================
id | product_id | date
--------------------------
1  | 1          | today
2  | 1          | today
3  | 2          | today

product_email_request

===================
id | product_id | date
--------------------------
1  | 1          | today
2  | 1          | today
3  | 2          | today

product_apply_request

===================
id | product_id | date
--------------------------
1  | 1          | today
2  | 2          | today
3  | 2          | today

and my goals is to achive something like this

product_id | view_count | email_count | apply_count
==========================================================
1          | 2          | 2           | 1
2          | 1          | 1           | 2

does it possible to do this on mysql ? i'm confused and i ever read that i should use union for this but still doesnt have the clue..

here is the sql script

    /*
Navicat MariaDB Data Transfer

Source Server         : LOKAL
Source Server Version : 100121
Source Host           : localhost:3306
Source Database       : test

Target Server Type    : MariaDB
Target Server Version : 100121
File Encoding         : 65001

Date: 2017-07-23 17:17:25
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for product_apply
-- ----------------------------
DROP TABLE IF EXISTS `product_apply`;
CREATE TABLE `product_apply` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) DEFAULT NULL,
  `creation` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of product_apply
-- ----------------------------
INSERT INTO `product_apply` VALUES ('1', '1', '2017-07-23 17:17:00');
INSERT INTO `product_apply` VALUES ('2', '2', '2017-07-23 17:17:08');

-- ----------------------------
-- Table structure for product_emails
-- ----------------------------
DROP TABLE IF EXISTS `product_emails`;
CREATE TABLE `product_emails` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) DEFAULT NULL,
  `creation` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of product_emails
-- ----------------------------
INSERT INTO `product_emails` VALUES ('1', '1', '2017-07-23 16:46:32');
INSERT INTO `product_emails` VALUES ('2', '1', '2017-07-23 16:46:47');
INSERT INTO `product_emails` VALUES ('3', '2', '2017-07-23 16:47:05');
INSERT INTO `product_emails` VALUES ('4', '2', '2017-07-23 16:47:47');
INSERT INTO `product_emails` VALUES ('5', '3', '2017-07-23 17:00:18');
INSERT INTO `product_emails` VALUES ('6', '1', '2017-07-23 17:00:29');

-- ----------------------------
-- Table structure for product_views
-- ----------------------------
DROP TABLE IF EXISTS `product_views`;
CREATE TABLE `product_views` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `creation` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of product_views
-- ----------------------------
INSERT INTO `product_views` VALUES ('1', '1', '2017-07-23 16:47:25');
INSERT INTO `product_views` VALUES ('2', '1', '2017-07-23 16:47:32');
INSERT INTO `product_views` VALUES ('3', '2', '2017-07-23 16:47:38');

any helps will be awesome.. thankyou



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire