create new column and set increment by step of 3 rows - mysql

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.

Related

insert into select max+1 in 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

Multiple row show in one row as sum in sql statement

ID |A |B
1 1 3
1 412 2
2 567 3
2 567 1
3 2 3
3 5 4
4 6 1
4 8 2
4 2 3
I want to get table:
ID |A |B
1 413 5
2 1134 4
3 7 7
4 16 6
As was pointed out, you want to use a group by clause for aggregate functions. In this case, you are summing the values for A and B, and grouping them by ID. The syntax is as follows:
SELECT ID, SUM(A), SUM(B)
FROM tablename
GROUP BY ID

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

How to select all records where a field is of a certain value until a record shows up that has a different value?

Let's say that we have a table with COLUMN1 and COLUMN 2. Here's a sample of the records:
COLUMN 1 | COLUMN 2
124 | 12
124 | 11
124 | 10
124 | 9
26 | 8
65 | 7
65 | 6
65 | 5
65 | 4
23 | 3
124 | 2
124 | 1
124 | 0
There is absolutely no pattern to this, but what I'd like to do is get:
COUNT(*) | COLUMN 1 | Smallest Column 2
4 | 124 | 9
1 | 26 | 8
4 | 65 | 4
1 | 23 | 3
3 | 124 | 0
So far, I've been doing this with PHP, but I'd like to find a way to do this in MySQL, as I'm sure it'd be a lot more efficient. The problem is, I can't even think of where to start with this. A regular GROUP BY COLUMN 1 wouldn't work because I want two results for 124, since it appears in two different instances. I've been fiddling around for hours and looking into the documentation and Google, but I haven't been able to find anything yet, and I was wondering if any of you would be able to point me in the right direction. Is this even possible with MySQL?
Well, it took a bit of fiddling, but here it is!
This assumes you have an id column in your table that you order by to get a consistent ordering (if you don't have an id column, order by timestamp or whatever in the inner query).
set #prev := '', #low := 0, #cnt := 0, #grp :=0;
select cnt, column1, low
from (
select
column2,
#low := if(#prev = column1, least(column2, #low), column2) low,
#cnt := if(#prev = column1, #cnt + 1, 1) cnt,
#grp := if(#prev = column1, #grp, #grp + 1) grp,
#prev := column1 column1
from (select column1, column2 from so9091342 order by id) x
order by grp, cnt desc) y
group by grp;
Here's the sql needed to set up a table for testing:
create table so9091342 (id int primary key auto_increment, column1 int, column2 int);
insert into so9091342 (column1, column2) values (124,12),(124,11),(124,10),(124,9),(26,8),(65,7),(65,6),(65,5),(65,4),(23,3),(124,2),(124,1),(124,0);
Output of above query:
+------+---------+------+
| cnt | column1 | low |
+------+---------+------+
| 4 | 124 | 9 |
| 1 | 26 | 8 |
| 4 | 65 | 4 |
| 1 | 23 | 3 |
| 3 | 124 | 0 |
+------+---------+------+
p.s. I named the table so9091342 because this is SO question ID #9091342.
Interesting question. I know Oracle much better than MySQL so I was able to get it working in Oracle. Might be a better way but this is what I came up with.
select count(col1) as cnt, col1, min(col2) as smallestCol2
from (
select col1, col2, col2-rnk as rnk
from
(
select col1, col2, RANK() OVER (PARTITION by col1 order by col2 asc) as rnk
from tmp_tbl
)
)
group by col1,rnk
order by min(col2) desc
I'm not quite sure how rank and partition work in MySQL but this might be helpful:
Rank function in MySQL
EDIT: To clarify what is going on in my query:
The inner query assigns a unique counter (RNK) to each value in column 1. The result of the most inner query is:
COL1 COL2 RNK
23 3 1
26 8 1
65 4 1
65 5 2
65 6 3
65 7 4
124 0 1
124 1 2
124 2 3
124 9 4
124 10 5
124 11 6
124 12 7
By subtracting the rank from column 2, you can get a unique value for each grouping of column 1 values. The result of the second nested query is:
COL1 COL2 RNK
23 3 2
26 8 7
65 4 3
65 5 3
65 6 3
65 7 3
124 0 -1
124 1 -1
124 2 -1
124 9 5
124 10 5
124 11 5
124 12 5
Then you can group on column 1 and that unique value. The final result:
CNT COL1 SMALLESTCOL2
4 124 9
1 26 8
4 65 4
1 23 3
3 124 0

Mysql left join or much simpler way?

I need to order data according to order index from other table. and order the data with the same 'id' according to entry date.
i cant figure it out how to join data and order using mysql command.
Table1
id name order
1 Ali 1
2 Cenk 3
3 Tan 2
Table 2
id tid m date
1 232 msj1 3
2 434 msj2 2
1 453 msj4 1
3 455 msj5 2
2 541 msj6 4
1 234 msj7 2
3 132 msj8 6
Needed query result
id tid m date
1 453 msj4 1
1 234 msj7 2
1 232 msj1 3
3 455 msj5 2
3 132 msj8 6
2 434 msj2 2
2 541 msj6 4
This should work:
select t2.id, t2.tid, t2.m, t2.date
from t2
left join t1 on t2.id=t1.id
order by t1.order
This orders by the ordering field from table 1.