insert into select max+1 in mysql - mysql

It has two tables.
Using the data from table B, we try to insert to table A.
However, the results I intended do
A
mid itemcode seq
1 101 1
1 101 2
1 102 3
2 101 1
2 102 2
B
mid itemcode seq
1 101 1
1 102 1
2 101 1
2 102 1
3 101 1
the result that I want.
mid itemcode seq
1 101 1
1 101 2
1 102 3
1 101 4
1 102 5
2 101 1
2 102 2
2 102 3
2 102 4
3 101 1
query
insert into A (memberid,itemcode,seq)
select B.memberid, B.itemcode, max(A.seq)+1 from A A ,B B where A.memberid=B.memberid group by B.memberid, B.itemcode;
and wrong result.
mid itemcode seq
1 101 1
1 101 2
1 102 3
1 101 4
1 102 4
2 101 1
2 102 2
2 102 3
2 102 3
3 101 1
Is there any good way?

Check the row_number() function, it might help you
insert into tab_a (mid,itemcode,seq)
select B.mid,
B.itemcode,
ifnull(max(A.seq), 0) + row_number() over(partition by mid)
from tab_a A
right join tab_b B
on A.mid = B.mid
group by B.mid, B.itemcode;
see db_fiddle

Related

SQL merge a table on itself [duplicate]

WEEK STUDENT CLASS TEST SCORE
1 1 A 1 93
1 1 A 2 97
1 1 B 1 72
1 1 B 2 68
1 1 C 1 93
1 1 C 2 51
1 1 H 1 19
1 2 A 1 88
1 2 A 2 56
1 2 B 1 53
1 2 B 2 79
1 2 C 1 69
1 2 C 2 90
1 2 H 1 61
1 3 A 1 74
1 3 A 2 50
1 3 B 1 76
1 3 B 2 97
1 3 C 1 55
1 3 C 2 63
1 3 H 1 63
2 1 A 1 59
2 1 A 2 68
2 1 B 1 77
2 1 B 2 80
2 1 C 1 52
2 1 C 2 94
2 1 H 1 74
2 2 A 1 64
2 2 A 2 74
2 2 B 1 92
2 2 B 2 98
2 2 C 1 89
2 2 C 2 84
2 2 H 1 54
2 3 A 1 51
2 3 A 2 82
2 3 B 1 86
2 3 B 2 51
2 3 C 1 90
2 3 C 2 72
2 3 H 1 86
I wish to group by STUDENT and WEEK and find the MAXIMUM(SCORE) value when TEST = 1. Then I wish to add the corresponding rows for CLASS and also the score for TEST = 2 based to get this:
WEEK STUDENT CLASS TEST1 TEST2
1 1 A 93 97
2 1 A 88 56
1 2 B 76 97
2 2 B 77 80
1 3 B 92 98
2 3 C 90 72
This is what I try but in SQL I am no able to SELECT columns which I don't group by
SELECT STUDENT, WEEK, CLASS, MAX(SCORE)
FROM DATA
WHERE TEST = 1
GROUP BY (STUDENT, WEEK)
but I do not find a solution that works.
Write a subquery that gets the highest score for each week and student on test 1. Join that with the table to get the rest of the row for that same score.
Then join that with the table again to get the row for the same student, week, and class, but with test = 2.
SELECT t1.week, t1.student, t1.class, t1.score AS test1, t3.score AS test2
FROM yourTable AS t1
JOIN (
SELECT week, student, MAX(score) AS score
FROM yourTable
WHERE test = 1
GROUP BY week, student
) AS t2 ON t1.week = t2.week AND t1.student = t2.student AND t1.score = t2.score
JOIN yourTable AS t3 ON t3.week = t1.week AND t3.student = t1.student AND t3.class = t1.class
WHERE t1.test = 1 AND t3.test = 2
ORDER BY student, week
DEMO

How does MySql sort data when the data of the sort filed is duplicate?

How dose MySql sort data when the data of sort filed is duplicate?
The table:
CREATE TABLE `orderby_test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`sort1` bigint(20) NOT NULL,
`sort2` bigint(20) NOT NULL,
`a` bigint(20) NOT NULL ,
`b` bigint(20) NOT NULL ,
PRIMARY KEY (`id`),
KEY `idx_sort` (`sort1`, `sort2`)
) ENGINE=InnoDB AUTO_INCREMENT=1;
sql :
select id, sort1,sort2,a,b from orderby_test where sort1 = 1 and a = 1 and b = 1 order by sort2 desc limit 0, 1000
then insert data 3 times:
insert into orderby_test (sort1,sort2,a,b) values (1,3,1,1);
then select, result is:
id sort1 sort2 a b
1 1 3 1 1
2 1 3 1 1
3 1 3 1 1
the id is ASC
then insert data 20 times:
insert into orderby_test (sort1,sort2,a,b) values (1,3,1,1);
then select, result is:
id sort1 sort2 a b
12 1 3 1 1
23 1 3 1 1
22 1 3 1 1
21 1 3 1 1
20 1 3 1 1
19 1 3 1 1
18 1 3 1 1
17 1 3 1 1
16 1 3 1 1
15 1 3 1 1
14 1 3 1 1
13 1 3 1 1
1 1 3 1 1
11 1 3 1 1
10 1 3 1 1
9 1 3 1 1
8 1 3 1 1
7 1 3 1 1
6 1 3 1 1
5 1 3 1 1
4 1 3 1 1
3 1 3 1 1
2 1 3 1 1
id is not sorted!
why?
if you do more insert
the result :
select id, sort1,sort2,a,b from orderby_test where sort1 = 1 and a = 1 and b = 1 order by sort2 desc limit 0, 20
id sort1 sort2 a b
282 1 3 1 1
281 1 3 1 1
280 1 3 1 1
279 1 3 1 1
278 1 3 1 1
277 1 3 1 1
276 1 3 1 1
275 1 3 1 1
274 1 3 1 1
273 1 3 1 1
272 1 3 1 1
271 1 3 1 1
270 1 3 1 1
269 1 3 1 1
268 1 3 1 1
267 1 3 1 1
266 1 3 1 1
265 1 3 1 1
259 1 3 1 1
258 1 3 1 1
id is sorted again!
because the implement of B+ tree in innodb? how does mysql do it?
mysql version : 5.7.21-21-log
How does MySql sort data when the data of the sort filed is duplicate?
It doesn't.
Once MySQL has enough information to arrange the result-set in such a way that it complies with the ORDER BY clause, it just packs the rows with ties as fast as it can, not caring at all about its relative order.
This is not specific to MySQL, it's how SQL is designed to behave. Sorting has a cost (time, CPU, memory...); there's no point in paying such cost when sorting is not needed.
You need to specify the second ordering preference:
...ORDER BY sort2 DESC, id ASC ...

Limit up to 10 ids from table 1 and get all the records from table 2 which matches the ids of table 1

I have a table1 for example:
orderID userId orderName orderTime...
1 11
2. 12
3. 11
4. 14
5. 11
6. 13
7. 11
8. 15
9. 16
10. 11
... ...
I have another table table2:
table2ID orderID item price ....
101 1 Apple 1.99
102 1 Banana 2.99
103 1 Grapes 0.99
104 4 pizza 6.99
105 4 drink 0.99
105. 3 chicken 1.99
106. 3 apple 1.99
I have tried this :
SELECT a.*, b.* FROM `table1` a
RIGHT JOIN table2 b on a.orderID = b.orderID
WHERE a.userID = 11 order by a.`orderTime` DESC LIMIT 25;
I want to get upto 10 unique orderIDs from table 1 of user 11 and all the details of that 10 ids from table 2. If I do LIMIT 25 then I don't get all the information.
I want my output as:
orderID userId orderName orderTime... table2ID orderID item price
1 11 101 1 Apple 1.99
1 11 102 1 Banana 2.99
1 11 103 1 Grapes 0.99
3 11 105 3 chicken 1.99
3 11 106 3 apple 1.99
SELECT a.*, b.*
FROM ( SELECT *
FROM a
WHERE a.userID = 11
LIMIT 10) as a
JOIN b
ON a.orderID = b.orderID

create new column and set increment by step of 3 rows

I have table1, i want get table2 ? increment by step of 3 rows.
I want create same |B value for first 3 rows, then increment +1 for second's 3 rows
table1
ID |A
1 125
2 412
3 567
4 567
5 485
6 458
7 656
8 856
9 456
table2
ID |A |B
1 125 101
2 412 101
3 567 101
4 567 102
5 485 102
6 458 102
7 656 103
8 856 103
9 456 103
In MySQL you can use something like this:
SET #c := -1;
SELECT id, A, (#c := #c+1) DIV 3 + 101 AS B FROM table1
You can use either create table from select or insert from select.
You can achieve it using MySql's mathematical functions, e.g.:
select t.id, t.a, 101 + floor((#rn:=#rn+1)/3) as B
from temp t, (SELECT #rn:=-1) t2;
Here is the SQL Fiddle.

Update value in a column based on another column in the same table in MYSQL

I have a table that is having 3 columns
vid - auto increment column
video_id - containing numbers
a_id - containing junk numbers
The table looks like below.
Vid Video_id a_id
101 1 3
102 1 3
103 5 3
104 5 3
105 5 3
106 11 3
107 11 3
108 11 3
109 11 3
110 11 3
I want to update a_id column values based on video_id values. Values in a_id should be updated as below.ex: If there are five 11 digit in video_id then the value in a_id should be updated 1 through 5.
Vid Video_id a_id
101 1 1
102 1 2
103 5 1
104 5 2
105 5 3
106 11 1
107 11 2
108 11 3
109 11 4
110 11 5
You can use user defined variables to give rank for each video group and then join with your real table by your auto increment column and update a_id accordingly
update t
join (
SELECT
Vid,
#r:= CASE WHEN Video_id = #g THEN #r+1 ELSE #r:=1 END a_id
,#g:=Video_id
FROM t,(SELECT #r:=0,#g:=0) t1
ORDER BY Video_id
) t1
on(t.Vid =t1.Vid)
set t.a_id = t1.a_id
Demo