I am trying to build a query to select the most sold products given the number of top products to fetch and the language code. I don't know to do it right so I would appreciate you help.
shop_transactions
id
1
2
3
shop_transaction_products_match
id | shop_transaction_id | product_id | units_bought
1 1 1 4
2 2 2 1
3 3 2 2
products_translations
id | product_id | language_code | name | seo_name
1 1 es Hola hola
2 1 en Hey u hey-u
3 2 es Adiós adios
4 2 en Bye u bye-u
products
id | category_id
1 1
2 2
product_categories
id
1
2
product_categories_translations
id | category_id | language_code | name | seo_name
1 1 es AA aa
2 1 en BB bb
3 2 es CC cc
4 3 en DD dd
The information per row is: name from product_translations, seo_name from product_translations, name from product_categories_translations, seo_name from product_categories_translations
Rows ordered descendant by number products sold in total.
Considering the example information, the result if number of products to fetch is 2 and the language is es, would be:
Hola, hola, AA, aa -> 4 units sold of this product
Adiós, adios, CC, cc -> 3 units sold of this product
Thank you!
Edit:
The code I tried so far... and I am missing some things that I dont know how to do it:
SELECT pt.name, pt.seo_name, pct.name as name_category, pct.seo_name as seo_name_category
From product_translations pt, products p, product_categories pc, product_categories_translations pct
where p.id in (Select product_id from shop_transaction_product_match where shop_transaction_id in
(Select id from shop_transactions)) AND pt.language_code = :language_code AND pct.language_code = :language_code
AND p.category_id = pct.category_id
Try this one:
SELECT DISTINCT
pt.name, pt.seo_name, pct.name, pct.seo_name,
SUM(stpm.units_bought) units_sold
FROM products AS p
LEFT JOIN product_categories AS pc ON p.category_id = pc.id
LEFT JOIN products_translations AS pt ON p.id = pt.product_id AND pt.language_code = 'es'
LEFT JOIN product_categories_translations AS pct ON pc.id = pct.category_id AND pct.language_code = 'es'
LEFT JOIN shop_transaction_products_match AS stpm ON p.id = stpm.product_id
-- Where p.id = 1
GROUP BY p.id
ORDER BY units_sold DESC
limit 2
Instead of using IN it is better to use INNER JOIN.
SELECT products_translations.name,
products_translations.seo_name,
product_categories_translations.name,
product_categories_translations.seo_name,
SUM(shop_transaction_products_match.units_bought) AS Product_count
FROM
shop_transactions
INNER JOIN
shop_transaction_products_match
ON shop_transactions.id=shop_transaction_products_match.shop_transaction_id
INNER JOIN
products_translations
ON products_translations.product_id = shop_transaction_products_match.product_id
INNER JOIN
products
ON products.id = shop_transaction_products_match.product_id
INNER JOIN
product_categories
ON products.category_id = product_categories.id
INNER JOIN
product_categories_translations
ON product_categories_translations.category_id = product_categories.id AND product_categories_translations.language_code = products_translations.language_code
WHERE products_translations.language_code= 'es' GROUP BY products.id
Limit 2
Hope this helps.
Related
SELECT li_1.carrier, li_1.product_id, li_1.quantity, products_description.products_name, sites.sites_id, sites.sites_name, counted_table.counted
FROM inventory li_1
INNER JOIN products_description ON li_1.product_id = products_description.products_id
INNER JOIN sites ON products_description.data = sites.data
INNER JOIN (
SELECT SUM( li_2.quantity ) AS counted
FROM inventory li_2
WHERE li_1.product_id = li_2.product_id
) counted_table
GROUP BY li_1.product_id
ORDER BY li_1.id DESC
I'm attempting to use the parent id (product_id) to count the total amount of quantity for each product in the subquery - But I only get the standard mysql error message.
So something like
id | quantity | total
---------------------------------
0001 | 2 | 6
| |
0001 | 4 | 6
What could be wrong?
if you change sub-query, it could be fixed
SELECT li_1.carrier, li_1.product_id, li_1.quantity, products_description.products_name, sites.sites_id, sites.sites_name, counted_table.counted
FROM inventory li_1
INNER JOIN products_description ON li_1.product_id = products_description.products_id
INNER JOIN sites ON products_description.data = sites.data
INNER JOIN (
SELECT product_id,SUM( li_2.quantity ) AS counted
FROM inventory li_2
GROUP BY li_2.product_id
) counted_table ON li_1.product_id = counted_table.product_id
ORDER BY li_1.id DESC
I am trying to get a count(*) for different column from a different table using union.
//tbl_churidar
order_id order_no_first order_no
--------------------------------------
1 C 1000
2 C 1001
3 C 1002
//tbl_anarkali
order_id order_no_first order_no
--------------------------------------
1 A 1003
2 A 1004
3 A 1005
//tbl_assign
assign_id order_id order_no_first
---------------------------------------
1 1 C
2 1 A
3 2 C
4 3 C
5 2 A
6 3 A
//tbl_unit_status
status_id assign_id status_status stitching_worker
-----------------------------------------------------------
1 1 Stitch AA
2 2 QC {null}
3 3 Stitch BB
4 4 Stitch BB
5 5 Stitch AA
6 6 Stitch CC
from the table tbl_unit_status where status_status = Stitch should INNER JOIN with other two table and get the total count of churidar and anarkali each stitching_worker taken.
the required output is,
churidar anarkali stitching_worker
----------------------------------------
1 1 AA
2 0 BB
0 1 CC
I have tried to get the above output but got stuck. Below is my code,
SELECT churidar, anarkali, stitching_worker
FROM ((
SELECT count(*) AS churidar, NULL AS anarkali,
us.stitching_worker
FROM tbl_unit_status us
INNER JOIN tbl_assign a ON a.assign_id = us.assign_id
INNER JOIN tbl_churidar o ON
(o.order_id = a.order_id AND
o.order_no_first = a.order_no_first)
INNER JOIN tbl_contacts c ON c.contacts_id = o.contacts_id
LEFT JOIN tbl_title t ON t.title_id = c.title_id
WHERE us.status_status = "Stitch" AND
o.order_no_first = "C"
GROUP BY us.stitching_worker
)
UNION (
SELECT NULL AS churidar, count(*) AS anarkali,
us.stitching_worker
FROM tbl_unit_status us
INNER JOIN tbl_assign a ON a.assign_id = us.assign_id
INNER JOIN tbl_anarkali o ON (
o.order_id = a.order_id AND
o.order_no_first = a.order_no_first)
INNER JOIN tbl_contacts c ON c.contacts_id = o.contacts_id
LEFT JOIN tbl_title t ON t.title_id = c.title_id
WHERE us.status_status = "Stitch" AND
o.order_no_first = "A"
GROUP BY us.stitching_worker
)
) AS T1
the output for the above code is,
churidar anarkali stitching_worker
----------------------------------------
1 0 AA
{null} 1 AA
2 0 BB
0 1 CC
how to get the required output. I have tried a lot. Help me find the answer. Thankyou.
If I understand correctly (which I may not), you don't need the first two tables. You can get the information you need from tbl_assign and just use aggregation:
select us.stitching_working,
sum(a.order_no_first = 'C') as churidar,
sum(a.order_no_first = 'A') as anarkali
from tbl_unit_status us join
tbl_assign a
on us.assign_id = a.assign_id
where us.status_status = 'Stitch'
group by us.stitching_working;
Below is the query I am using to get the team information. However, I want also display both usernames assorted with the team. The database is setup using two tables. One contains all of the user information and the other (userLeagues) contains a set of ids, such as userID and teamID.
Example - userLeagues
leagueID | userID | teamID
1 1 1
1 5 1
1 10 2
2 1 1
2 8 1
This is result I am trying to achieve.
leagueID | TeamID | userID1| userID1
1 1 1 5
This would then be outputed as :
leagueID | TeamID | userID1 | userID1
1 Mad Racing driver 1 dirver 5
This is the code I have so far which gets the team but how do I get both driers and their username associated with the same team?
SELECT
u.username
, t.teamName
, t.teamImage
/* File Table */
, f.fileType
FROM
userleague ul
INNER JOIN users u ON u.userID = ul.userID
LEFT JOIN teams t ON t.teamID = ul.teamID
LEFT JOIN fileType f ON f.fileID = t.fileID
WHERE leagueID = 1
GROUP BY ul.teamID
You can do this with min() and max(), if there are only two values:
SELECT t.teamName, t.teamImage, f.fileType,
min(u.username) as user1,
(case when max(u.username) > min(u.username) then max(u.username) end) as user2
FROM userleague ul INNER JOIN
users u
ON u.userID = ul.userID LEFT JOIN
teams t
ON t.teamID = ul.teamID LEFT JOIN
fileType f
ON f.fileID = t.fileID
WHERE leagueID = 1
GROUP BY t.teamName, t.teamImage, f.fileType;
I'm here with this (I'm sure it is) simple question I can't figure out how to solve.
I have this schema:
With this data:
My expected result is:
For "JOHN NASH":
PERSON_NAME | TOTAL_FRUIT | TOTAL COOKIE
----------------------------------------
JOHN NASH | 10 | 38
For "OSCAR WILDE":
PERSON_NAME | TOTAL_FRUIT | TOTAL COOKIE
----------------------------------------
OSCAR WILDE | 28 | 0
Thanks in advance.
SELECT name, IFNULL(f.total, 0) AS total_fruit, IFNULL(c.total, 0) AS total_cookie
FROM person AS p
LEFT JOIN (SELECT person_idperson, SUM(cost) AS total
FROM fruit
GROUP BY person_idperson) AS f
ON p.idperson = f.person_idperson
LEFT JOIN (SELECT person_idperson, SUM(cost) AS total
FROM cookie
GROUP BY person_idperson) AS c
ON p.idperson = c.person_idperson
SELECT p.name AS PERSON_NAME,
IFNULL(SUM(f.cost),0) AS TOTAL_FRUIT,
IFNULL(SUM(c.cost),0) AS TOTAL_COOKIE
FROM person AS p
LEFT JOIN fruit as f
ON p.idperson = f.person_idperson
LEFT JOIN cookie as c
ON p.idperson = c.person_idperson
GROUP BY p.idperson
Table products:
id|name
-------
1 |computer
2 |microwave
3 |transl
Table product_features:
feature | id_product | feature_value
------------------------------------
count_of_buttons | 1 | 1
count_of_buttons | 2 | 2
count_of_buttons | 3 | 1
color | 1 | white
color | 2 | white
color | 3 | black
Pls, how to get all white products with one button?
Thank you very much!
select product.*
from product
join product_features as buttons
on buttons.id_product = product.id
join product_features as color
on color.id_product = product.id
where buttons.feature_value = '1'
and buttons.feature = 'count_of_buttons'
and color.feature_value = 'white'
and color.feature = 'color';
select
p.id
p.name
from
products p
join (select * from product_features where feature = 'color') colors on (p.id=colors.id_product)
join (select * from product_features where feature = 'count_of_buttons') buttons on (p.id=buttons.id_product)
where
colors.feature_value = 'white'
and buttons.feature_value = 1
You might consider reorganizing the product_features table so that you have a separate column for each feature (i.e. a color column and a count_of_buttons column) so that you have one row for each product. In fact it could all be in the products table.
SELECT
p.id
FROM products p
JOIN product_features pf
ON p.id = pf.id
WHERE pf.feature_value = 'white' AND pf.count_of_buttons = 1
select p.id, p.name from products p inner join product_features ON p.id=id_product where feature_value='white' and feature='color' and count_of_buttons=1