Raw SQL query/django query : Get column info by grouping - mysql

Following are the tables:
Product Table
25 Tshirt
26 Dress
ProductVariation Table
id product_id variation_id vendor_id
46 26 47 1
47 26 48 1
48 26 49 1
49 27 50 1
Variation Table
id value attribute_id category_id variationinfo_id
47 Female 2 1 17
48 89 3 1 17
49 90 1 1 18
50 #343434 2 1 18
VariationInfo Table
id stock remarks
17 8 remarks1
18 10 remarks2
Attribute Table
id attribute_name
1 size
2 color
3 gender
Category Table
id name parent remarks
1 bla bla bla
Problem scenario:
Product 26 has variation_ids 47, 48, 49. Of these, 47 and 48 have same variationinfo_id 17 and another 49, has 18. For product 26, total stock information is obtained as 18.
However, I need to get distributed stock information for different variationinfo_ids, For example: For variation(VariationTable) 47, 48 with same variationinfo_id of 17, total stock of this variation -> 8 and for 48 with variationinfo_id of 18, total stock of this variation -> 10.
How do I write query for the latter part?

select temp.product_id,temp.variation_ids,temp.variationinfo_id,vi.stock
(select p.product_id,group_concat(v.id) as variation_ids, v.variationinfo_id
from ProductVariation p
join Variation v on p.variation_id=v.id
group by p.product_id,v.variationinfo_id) temp
join VariationInfo vi on temp.variationinfo_id=vi.id
Note: Code is not tested. Small syntactic error may exist.

Related

MySQL seems to ignore my WHERE clause with inner joins

In order to display data (orders or bids) in a excel-like grid, i fetch from my database with the help of this query. In this case, the order_bid table store the lines that compose an order (a line for each item), the item table defines the price of an item, the current sale the item is displayed on, and the lot table defines the item itself (name, description_ect...)
Here is the request i make, orders are another table but the important part is that an order_bid is linked to an order, and orders are linked to a unique sale, but it seems to ignore the WHERE close, so i get a lot more results than expected (for example if a client placed a bid for lot 4 of sale 1, i would see a bid for sale 2 lot 4, and so on for all sales that have a lot number 4 (so all of them basically))
SELECT o.id, o.order_id, o.lot_num, o.bid, i.lot_id, i.sale, i.price, l.description_fr
FROM order_bid o
INNER JOIN item i
ON o.lot_num = i.lot_num
INNER JOIN lot l
ON l.id = i.lot_id
WHERE o.order_id = 38
For example i expect for a request a result like:
id
order_id
lot_num
bid
lot_id
sale
price
description_fr
110
38
17
135
19
sale1
135
description1
111
38
21
83
23
sale1
183
description2
109
38
10
100
790
sale1
100
description3
but i get:
id
order_id
lot_num
bid
lot_id
sale
price
description_fr
110
38
17
135
19
sale1
135
description1
111
38
21
83
23
sale1
183
description2
111
38
21
183
11968
sale11
150
description4
109
38
10
100
790
sale1
100
description3
and a lot of other lots that i shouldn't be getting from other sales
and i verified, when i request all the order_bid with order_id = 38 (SELECT * FROM order_bid WHERE order_id = 38) i get:
id
order_id
lot_num
bid
109
38
10
100
110
38
17
135
111
38
21
183
summarized: i get info from multiple sales when i'm supposed to get only from one (done using the order_id filter)

SQL : loop through same table with tree structure data

I'm on MySQL 8.0.24, and I have a tree structure table named category, it has 3 columns: category_id, parent_id, image.
category_id
image
parent_id
69
68
70
68
71
68
60
57
61
60
62
57
63
62
64
57
65
64
66
57
67
66
68
57
53
52
54
52
55
52
56
52
57
41
58
57
59
58
18
14
19
18
20
18
21
1
22
21
23
22
24
22
25
21
26
25
27
26
28
26
I want to use the resultset of category_id obtained from another query, loop thru each element in the array and try to see if a given parent_id can be matched.
For example, the resultset of category_id is {98, 60, 26}, and I want to find out if any of the 3 elements has a parent_id of 1.
Analysis:
98's parent_id is NULL,
60's parent_id is 57, 41
26's parent_id is 25, 21, 1
Therefore a hit is found.
I tried to understand and use the following SQL from sql loop thru same table, with no luck...
with aa_category(category_id) as (select category_id from aa_category union all select nplus1.category_id from aa_category as nplus1, aa_category where aa_category.category_id = nplus1.parent_id) select category_id from aa_category;
How do I write this SQL?
you can use recursive cte:
with recursive cte as (
select *, category_id originalCategory from category
where category_id in (98, 60, 26)
union all
select c.*, cte.originalCategory from category c
join cte on cte.parent_id = c.category_id
)
select originalCategory,parent_id
from cte
where parent_id = 1
originalCategory
parent_id
26
1
db<>fiddle here

Select data basis of two values of one column matches of a table

suppose this is my table structure of table user
id field_id user_id value
1 1 37 Lalit
4 2 37 Test
5 13 37 123
6 18 37 324
7 28 37 english
8 33 37 203
9 21 37 201
10 1 39 Mukesh
11 2 39 Test
12 13 39 523
13 18 39 245
14 28 39 French
15 33 39 278
16 21 39 2897
So I wnat to get the result to match the two or three values from the column value and want the result
I made query like
SELECT DISTINCT user_id FROM user where value =123 AND value=523;
But it is not working please give solution how we get the result
A value in a row, as per your example, cannot be both 123 and 523. You have to use OR
SELECT DISTINCT(user_id) FROM user WHERE value=123 OR value=523;
Alternatively you can also use IN clause
SELECT DISTINCT user_id
FROM user
WHERE value IN (123, 523);

Find lowest value from the rows which contains similar foreign key value

Table:
pk fk price
1 1 23
2 1 12
3 1 3
4 2 53
5 2 75
6 3 95
7 3 113
8 3 63
9 3 73
10 3 93
11 4 113
11 4 150
11 4 105
In the above table:
How to find out the lowest price based on it's common fk value.For example: lowest price for fk=1 is 3, for fk=2 is 53, for fk=3 is 63 and for fk=4 is 105.
I want a single SQL statement which could find lowest price for each common fk value.
You just want a basic aggregate per group.
select fk, min(price)
from your_table
group by fk;

count items in col as rows MYSQL

Dear developers and programmers,
I have a tabel with SHOP_ID, PROD_ID, ipadres and some more tables, but these are where its all about...
Looks like this:
id ip number timestamp shop_id prod_id
--------------------------------------------------
42 81.69.205.25 1319488326 2 3
43 81.205.141.48 1319492649 2 3
44 193.58.10.10 1319520579 14 17
45 84.28.22.226 1319529529 11 19
46 88.15.81.188 1319543745 2 1
47 178.17.241.191 1319563031 14 7
48 87.28.107.171 1319563038 2 6
49 80.156.47.144 1319572818 14 7
50 82.76.241.175 1319577506 11 1
51 82.76.241.175 1319577584 13 1
52 82.76.241.175 1319577785 14 1
53 82.76.241.175 1319577860 4 1
54 62.94.133.153 1319579221 14 1
55 62.94.133.153 1319579281 2 3
56 77.70.175.221 1319617238 11 1
57 77.70.175.221 1319621845 13 1
58 77.70.175.221 1319621848 2 1
59 77.70.175.221 1319621850 11 1
.... more
--------------------------------------------------------
Is there a way to see for each prod_id how many ip numbers there excist for each shop id?
output example
1 2 3 4 5
---------------------------------------------
1 18 5 51 8 4
2 58 5 45 3 4
3 7 6 31 9 2
where horizontal is the prod_id and vertical is the shop_id
I've don this already:
select
shop_id,
count(distinct(ipadres)) amount
from table
GROUP BY shop_id
order by amount desc
but this wil give me only the result of all prod_id's combined.
I would like to have the prod_id's seperate in columns.
I hope there is a solution!
Kind Regards
Try this
select `prod_id`,shop_id,
count(distinct(ipadres)) amount
from table
GROUP BY shop_id,prod_id
order by amount desc