How to Update a Field From The Same Data in MySQL - mysql

id product main_image
--- ---------- -----------
1 1 0
2 1 0
3 1 0
4 2 0
5 2 0
6 2 0
7 3 0
8 3 0
9 3 0
10 3 0
I want to use mysql query to make table field datas (for main_image) like this
id product main_image
--- ---------- -----------
1 1 1
2 1 0
3 1 0
4 2 1
5 2 0
6 2 0
7 3 1
8 3 0
9 3 0
10 3 0
how can I do it? I want to set a main image for products.
I have about 3 millions record, I tried php but its too slow

You can use a subquery to select the smaller id form the table:
UPDATE myTable
SET main_image = 1
WHERE id IN
(SELECT MIN(temp.id)
FROM (SELECT * FROM myTable) AS temp
GROUP BY temp.product)

This...
UPDATE my_table x
JOIN
( SELECT product
, MIN(id) min_id
FROM my_table
GROUP
BY product
) y
ON y.product = x.product
AND y.min_id = x.id
SET x.main_image = 1;
... or just this ...
UPDATE my_table x
JOIN
( SELECT MIN(id) min_id
FROM my_table
GROUP
BY product
) y
ON y.min_id = x.id
SET x.main_image = 1;

Related

Counting unique pairs, but applying that count to all of its entries in SQL (not removing duplicates)

Let's say I have a table:
ID A B
10 0 0
11 0 0
12 0 1
13 0 1
14 1 1
15 1 1
16 1 1
And I want my table output to be:
ID A B A_B_COUNT
10 0 0 2
11 0 0 2
12 0 1 2
13 0 1 2
14 1 1 3
15 1 1 3
16 1 1 3
but with the code I have here my output looks like this
SELECT ID, COUNT(*) AS A_B_COUNT
FROM table
GROUP BY A, B
ID A B A_B_COUNT
10 0 0 2
12 0 1 2
14 1 1 3
Any way I can create an sql query to is like my top table vs the one I make currently
Using: 10.5.5-MariaDB
Use window functions:
select t.*, count(*) over (partition by a, b) as a_b_count
from t;
You can do:
select e.A, e.B, c.cnt as A_B_COUNT
from entries e
join (
select A, B, count(*) as cnt
from entries
group by A, B
) as c
where e.A=c.A and e.B=c.B
See db-fiddle.

subtract from values by groups

What is the best way to subtract the lowest value from all values by group?
Something like:
ID Name Value
1 A 10
1 B 40
1 C 100
2 A 20
2 B 80
2 C 90
3 A 4
3 B 7
3 C 8
turn to:
ID Name Value
1 A 0
1 B 30
1 C 90
2 A 0
2 B 60
2 C 70
3 A 0
3 B 3
3 C 4
Use window functions:
select id, name, value - min(value) over (partition by id) as
from t;
If you actually want to update the values, in MySQL, aggregation and join are probably the simplest solution:
update t join
(select id, min(value) as min_value
from t
group by id
) tt
on t.id = tt.id
set value = value - min_value
where min_value <> 0;

Mysql selecting rows

I have a database scheme with a table like this:
id --- username --- sex
1 A 1
2 D 2
3 F 1
4 G 2
5 H 1
6 x 1
7 r 1
I want to select only 2 males and lets say 4 females, male is 1 and female is 2. How would we achieve that in one mysql query and if I have more var's to select by ?
(select * from your_table where sex = 1 limit 2)
union all
(select * from your_table where sex = 2 limit 4)

How to Put condition in order by sql Query

This is my table name :- xyztable and in this table i have 3 column
A:- id
B:- Name
c:- Order_number
i have a sample data like
Id Name order_nmber
1 A 1
2 K 0
3 B 6
4 c 3
5 P 0
6 d 5
7 e 2
8 O 0
I wrote the query is
select * from xyztable order by order_number ASC;
that give me the result is
Id Name order_nmber
2 K 0
5 P 0
8 O 0
1 A 1
7 e 2
4 c 3
6 d 5
3 B 6
But i want the result like :-
Id Name order_nmber
1 A 1
7 e 2
4 c 3
6 d 5
3 B 6
2 K 0
5 P 0
8 O 0
So how can i get this in single query....Please help!!!!
You could do like below:
select * from xyztable order by order_number = 0, order_number ASC;
select * from xyztable
order by case when order_number=0 then 1 else 0 end ASC, order_number ASC;
Try something like this:
SELECT * FROM xyztable ORDER BY order_number = 0, order_number;

How do I count total of value in one column with different data

I'm a bit confuse how to name my question title.
Let say my MySQL database table like this. table_group
UserID UserGroup UserStatus
------------------------------
1 1 1
2 1 1
3 1 2
4 2 3
5 3 2
6 3 1
7 4 3
9 4 4
10 4 1
I want to group it by UserGroup and count the UserStatus.
Let me know what is the correct statement to get the result like this
UserGroup Status_1 Status_2 Status_3 Status_4
-------------------------------------------------
1 2 2 0 0
2 0 0 1 0
3 1 1 0 0
4 1 0 1 1
SELECT UserGroup, count(UserStatus = 1) as Status_1, count(UserStatus = 2) as Status_2,
count(UserStatus = 3) as Status_3, count(UserStatus = 4) as Status_4
FROM table_group GROUP BY UserGroup
You could use case to count rows that match a condition:
select UserGroup
, count(case when UserStatus = 1 then 1 end) as Status_1
, count(case when UserStatus = 2 then 1 end) as Status_2
, count(case when UserStatus = 3 then 1 end) as Status_3
, count(case when UserStatus = 4 then 1 end) as Status_4
from table_group
group by
UserGroup