mysql complicated inner join on many to many - mysql

I have 7 tables db_category, db_city, db_locality, db_shop, db_shop_locality, db_shop_recommended, db_shop_views.
db_category
category_id category_name publish
1 Apparel 1
db_city
city_id city_name publish
1 bangalore 1
db_locality
locality_id locality_name publish
1 kalyan nagar 1
2 Madiwala 1
db_shop
shop_id category_id city_id locality_id shop_name publish
1 1 1 1 name 1
2 1 1 2 name1 1
3 1 1 1 name2 1
4 1 1 1 name3 1
db_shop_recommended
recommended_id category_id shop_id priority publish
1 1 1 1 1
2 1 2 2 1
db_shop_views
views_id shop_id ip_addr publish
1 1 127.0.0.1 1
2 2 ::1 1
3 4 127.0.0.1 1
4 4 ::1 1
5 3 ::1 1
I want to join all the above tables. And the conditions for the join are,
display all the rows from db_shop when publish=1 and join db_category, db_city and db_locality with db_shop
in db_shop_recommended who's priority=1 comes first and so on.
and shop_id not in db_shop_recommended, count shop_id not in db_shop_recommended from db_shop_views who's shop_id got more views.
My result should looks like below,
shop_id category_name city_name locality_name shop_name
1 Apparel bangalore kalyan nagar name (result based on `db_shop_recommended` who's priority is more)
2 Apparel bangalore Madiwala name1 (result based on `db_shop_recommended` who's priority is more)
4 Apparel bangalore kalyan nagar name4 (result based on `db_shop_views` who's view count is more)
3 Apparel bangalore kalyan nagar name3 (result based on `db_shop_views`)
I have no idea how to join and count the views. Is it possible to join in above method.

After lots of searching. Finally i got the answer.
SELECT DISTINCT s.shop_id, c.city_name, l.locality_name, ca.category_name, s.shop_name, s.shop_logo, s.cart_url, s.shop_about FROM db_shop s INNER JOIN db_city c ON c.city_id = s.city_id INNER JOIN db_category ca ON ca.category_id = s.category_id INNER JOIN db_locality l ON l.locality_id = s.locality_id LEFT JOIN (SELECT * FROM db_shop_recommended r ORDER BY r.priority ASC) r1 ON s.shop_id = r1.shop_id LEFT JOIN (select distinct g.shop_id, g.cnt from (select distinct shop_id, count(shop_id) cnt from db_shop_views group by shop_id) g inner join (select max(s.cnt) max_cnt from(select distinct shop_id, count(shop_id) cnt from db_shop_views group by shop_id) s) m on m.max_cnt = g.cnt) v ON s.shop_id = v.shop_id WHERE s.publish = 1

Related

MYSQL Stuck Generating temp table (massive query)

I have 4 tables (1 to many):
Dont say anything about that "email" relation. It is how my developer boss built it years ago.
EMPLOYEES (+-50 results)
------------------------------------------------
id name
1 EmpName 1
2 EmpName 2
CUSTOMERS (+50k results)
------------------------------------------------
id name email employee_assigned
1 John john#doe.com 12
2 Donald donald#duck.com 6
INTERESTS_CATEGORIES (+650k results)
------------------------------------------------
id customer_email category_id
1 john#doe.com 97
2 john#doe.com 13
3 donald#duck.com 56
4 donald#duck.com 126
5 donald#duck.com 45
INTERESTS_PRODUCTS (+650k results)
------------------------------------------------
id customer_email product_id
1 john#doe.com 78
2 john#doe.com 23
3 donald#duck.com 19
4 donald#duck.com 56
5 donald#duck.com 45
So I need to filter the customers by their assigned employee and their interests.
And here is the query:
SELECT
*
FROM
(
SELECT
customers.id AS 'id',
customers.name AS 'first_name',
customers.email,
employees.id AS 'employee_id'
FROM
customers,
employees
WHERE
employees.id = 2
AND
customers.employee_assigned = employees.id
) AS myCustomers
LEFT JOIN interests_categories
ON interests_categories.customer_email = myCustomers.email
LEFT JOIN interests_products
ON interests_categories.customer_email = myCustomers.email
WHERE
(
interests_categories.category_id = 20
OR
interests_categories.category_id = 21
)
GROUP BY myCustomers.email
So, the problem:
If the employee has a low number of assigned customers (like 3) query
is successfull.
If the employee has a medium-high number of assigned customers (over 100) query stucks.
I execute SHOW PROCESSLIST and it is stucked "Generating temp table".
Anyone has idea? :(
Thank you.
Check the indexes on your tables and try this:
SELECT
c.id AS 'id',
c.name AS 'first_name',
c.email,
e.id AS 'employee_id'
ic.*,
ip.*
FROM customers c
JOIN employees e
ON c.employee_assigned = e.id
LEFT JOIN interests_categories ic
ON ic.customer_email = c.email
LEFT JOIN interests_products ip
ON ic.customer_email = c.email
WHERE
(
ic.category_id IN (20,21)
AND e.id = 2
)
GROUP BY myCustomers.email
Incidentally, a less dumb design might look like as follows. If it was me, I'd start with this, and provide properly representative CREATE and INSERT statements accordingly. Also, I'm curious about where category_id comes from - because that's potentially an area for further optimization.
EMPLOYEES
------------------------------------------------
employee_id name
6 EmpName 1
12 EmpName 2
CUSTOMERS
------------------------------------------------
customer_id name email employee_assigned
1 John john#doe.com 12
2 Donald donald#duck.com 6
INTERESTS_CATEGORIES
------------------------------------------------
customer_id category_id
1 97
1 13
2 56
2 126
2 45
INTERESTS_PRODUCTS
------------------------------------------------
customer_id product_id
1 78
1 23
2 19
2 56
2 45

How to select counts from different tables with foreign keys?

city
----------------------
id city_name
1 Pune
2 Mumbai
3 Banglore
Branches
----------------------
id branch_name city_id
1 Magarpatta 1
2 Wagholi 1
3 Kurla 2
4 CST 2
5 Thane 2
6 Anekal 3
Employees
----------------------
id employee_name city_id
1 Arun 1
2 Varun 1
3 Mahesh 2
4 Umesh 2
5 Prakash 1
6 Kedar 3
Expected result
-----------------------------
id(city) city_name No_of_Branch no_of_employee
1 Pune 2 3
2 Mumbai 3 2
3 Banglore 1 1
Can I get above result in single Query?
Can I Use Crosstab in this scenario? How?
SELECT c.*, count(DISTINCT b.id) AS No_of_Branch, count(DISTINCT e.id) AS no_of_employee
FROM city c
LEFT JOIN branches b ON (b.city_id=c.id)
LEFT JOIN employees e ON (e.city_id=c.id)
GROUP BY c.id
Or
SELECT c.*,
(SELECT count(b.id) FROM branches b WHERE b.city_id=c.id) AS No_of_Branch,
(SELECT count(e.id) FROM employees e WHERE e.city_id=c.id) AS no_of_employee
FROM city c
Try this:
SELECT c.id, c.city_name,
COUNT(b.id) AS No_of_Branch,
no_of_employee
FROM city c
LEFT JOIN branches b ON b.city_id = c.id
LEFT JOIN (
SELECT city_id, COUNT(e.id) AS no_of_employee
FROM employees AS e
GROUP BY e.city_id
) AS e_grp ON e_grp.city_id = c.id
GROUP BY c.id, c.city_name
The trick here is to use a derived table containing the count of employees per city_id. I you just join all tables together you will end up with duplicate counts of employees.
Demo here

How to show only the id of one table and not to duplicate with other tables?

I have 6 tables and 3 of them is what I need to show in my table and the other 3 is what I needed to hide in the same table.
Report:
id name branch comp_id start_date end_date
100 A 001 011 2022-08-14 2022-08-14
200 B 002 012 2022-08-14 2022-08-14
Report Details:
id product_id product_code price deliveries
100 01 11 20.00 10
100 01 11 20.00 10
200 01 11 20.00 20
200 02 12 25.00 20
Products:
id code name
01 11 Prod 1
02 12 Prod 2
Product Details:
id code name Desc
01 11 Prod 1 Desc 1
02 12 Prod 2 Desc 2
Branches:
id code name
001 021 Branch 1
002 022 Branch 2
Companies:
id name branch
011 Company 1 021
012 Company 2 022
I want the output to be like this:
id: will come from reports table
branch_name: will come from branches table using the branch in report table
company_name: will come company using the comp_id in report table
product_name: will come from products table using product_id in report details table
description: will come from product_details table using product_code in report details table
start_date: will come from report table
end_date: will come from report table
id branch_name company_name product_name description start_date end_date
100 branch 1 Company 1 Prod 1 Desc 1 2022-08-14 2022-08-14
200 branch 2 Company 2 Prod 1 Desc 1 2022-08-14 2022-08-14
200 branch 2 Company 2 Prod 2 Desc 2 2022-08-14 2022-08-14
I have this sql and it all shows the id in report details table:
SELECT *, `acc`.`name` AS `cname`, `out`.`name` AS `outname`, `pro`.`name` AS `pname`, `prod`.`name` AS `sname`
FROM `report` AS `rep`
JOIN `companies` AS `acc` ON `rep`.`account_id`=`acc`.`code`
JOIN `branches` AS `out` ON `rep`.`outlet_id`=`out`.`code`
JOIN `report_details` AS `red` ON `rep`.`report_id`=`red`.`report_id`
JOIN `products` AS `pro` ON `red`.`product_id`=`pro`.`id`
JOIN `product_details` AS `prod` ON `red`.`sku_id`=`prod`.`id`
Try this:
SELECT R.id, B.name AS branch_name, C.name AS company_name, PD.product_name AS product_name, PD.Desc AS description, R.start_date
FROM Report R
LEFT JOIN report_details RD ON R.id = R.id
LEFT JOIN products P ON RD.product_id = P.id
LEFT JOIN product_details PD ON PD.product_id = PD.id
LEFT JOIN branches B ON R.branch = B.id
LEFT JOIN companies C ON R.comp_id = C.id
GROUP BY R.id, B.name, C.name, PD.product_name, PD.Desc, R.start_date

MySQL returns incorrect data with use of join

I have join with a multiple tables. You can find the tables and MySQL query in this demo.
These are the tables:
users
id name
1 abc
2 xyz
3 pqr
friend
user_id friend_id
1 2
1 3
2 3
collection
id user_id friend_id amount
1 1 2 100
2 2 1 -100
3 2 3 200
4 3 2 -200
5 1 3 300
6 3 1 -300
bill
id use_id(bill_creator)
1 1
2 2
3 2
bill_person
id bill_id user_id
1 1 1
2 1 2
3 1 3
4 2 2
5 2 3
6 3 2
6 3 1
So far I have made this query:
SELECT mf.id
, mf.name
, c.amount AS amount,count(bp.user_id) AS no_common_bills
FROM (
SELECT fr.user_id AS user_id
, fr.friend_id AS friend_id
FROM friend fr
JOIN users fru
ON fru.id = fr.user_id
WHERE fru.id IN (1)
UNION
SELECT fl.friend_id AS user_id
, fl.user_id AS friend_id
FROM friend fl
JOIN users flf
ON flf.id = fl.friend_id
WHERE flf.id IN (1)
) f
JOIN users mf
ON mf.id = f.friend_id
LEFT
JOIN collection c
ON c.friend_id = mf.id
AND c.user_id = f.user_id
LEFT JOIN bill_person bp
ON bp.user_id=f.user_id AND c.friend_id = mf.id
GROUP BY mf.id
ORDER BY mf.id
and my output of this query is
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 2
but I want this result:
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 1
I am getting wrong output at NO_COMMON_BILLS. Other than that, all values are correct.
Third time's a charm...
http://sqlfiddle.com/#!2/338dd/12
SELECT u.name friend
, COUNT(bp2.user_id) no_common_bills
FROM users u
LEFT
JOIN
( SELECT user_id me, friend_id them FROM friend WHERE user_id = 1
UNION
SELECT friend_id,user_id FROM friend WHERE friend_id = 1
) x
ON x.them = u.id
LEFT
JOIN bill_person bp1
ON bp1.user_id = x.them
LEFT
JOIN bill_person bp2
ON bp2.bill_id = bp1.bill_id
AND bp2.user_id = x.me
WHERE u.id <> 1
GROUP
BY friend;
FRIEND NO_COMMON_BILLS
jkl 0
mno 0
pqr 1
xyz 2

table joins with multiple group_concat

I have a problem regarding joining tables with group_concat. Here are the details.
table_orders:
item_cd order_id descs quantity status seq_no
1 100 coca-cola 2 A 232
2 100 pizza 1 A 233
3 101 cheeseburger 5 A 234
4 102 pepsi 4 A 235
4
table_instructions:
item_cd instruction
3 more cheese
3 less vegetable
cancelled_item_table:
quantity seq_no
1 234
1 234
1 235
Now what I want to achieve is like this:
item_cd descs quantity instructions cancelled_item
1 coca-cola 2 - -
2 pizza 1 - -
3 cheeseburger 2 more cheese, less vegetable 1,1
4 pepsi 4 - 1
This is my current query:
SELECT
ord.item_cd,
ord.order_id,
ord.descs,
ord.quantity,
GROUP_CONCAT(x.quantity) as cancelled,
GROUP_CONCAT(i.instruction) as instruct
FROM table_orders ord
LEFT JOIN cancelled_item_table x ON ord.seq_no = x.seq_no
LEFT JOIN table_instructions i ON ord.item_cd = i.item_cd
WHERE ord.status = 'A'
GROUP BY ord.order_id
and here is the output:
item_cd descs quantity instructions cancelled_item
1 coca-cola 2 - 1
2 pizza 1 - 1
3 cheeseburger 2 more cheese, more cheese,
less vegetable, less vegetable 1,1,1,1
4 pepsi 4 - 1
If you notice, cheeseburger has 2 cancelled item and 2 instruction, but the output is 4, looks like it's multiplying.
Since the join with cancelled_item_table multiplies rows, you have to join to an already grouped subquery, like this:
SELECT
ord.item_cd,
ord.order_id,
ord.descs,
ord.quantity - coalesce(x.tot,0) as quantity,
GROUP_CONCAT(i.instruction) as instruct,
x.cancelled
FROM
table_orders ord LEFT JOIN table_instructions i
ON ord.item_cd = i.item_cd LEFT JOIN
(select seq_no, count(*) as tot, GROUP_CONCAT(quantity) as cancelled
from cancelled_item_table
group by seq_no) x ON ord.seq_no = x.seq_no
WHERE ord.status = 'A'
GROUP BY ord.item_cd, ord.order_id, ord.descs, quantity