I want to list every product in database with visitor tracking and sales info by user id.
Here is what I got so far:
http://www.sqlfiddle.com/#!2/ec506/19
SELECT products.product_name,
tracking.unique_hits,
tracking.total_hits,
COUNT(sales.product_id) as total_sales,
SUM(sales.amount) as total_revenue
FROM products
INNER JOIN tracking ON products.id = tracking.product_id
INNER JOIN sales ON products.id = sales.product_id
WHERE products.vendor_id = 0;
It outputs this:
| PRODUCT_NAME | UNIQUE_HITS | TOTAL_HITS | TOTAL_SALES | TOTAL_REVENUE |
|----------------|-------------|------------|-------------|---------------|
| test product 1 | 42 | 52 | 3 | 30 |
But I want it to output products without sales also, so it should output something like this:
| PRODUCT_NAME | UNIQUE_HITS | TOTAL_HITS | TOTAL_SALES | TOTAL_REVENUE |
|----------------|-------------|------------|-------------|---------------|
| test product 1 | 42 | 52 | 3 | 30 |
| test product 2 | 10 | 13 | 0 | 0 |
Or
| PRODUCT_NAME | UNIQUE_HITS | TOTAL_HITS | TOTAL_SALES | TOTAL_REVENUE |
|----------------|-------------|------------|-------------|---------------|
| test product 1 | 42 | 52 | 3 | 30 |
| test product 2 | 0 | 0 | 0 | 0 |
In case there is no even visitor tracking data at table.
I have no idea how to do it. Some help needed! :)