MySQL increment values of 1 - mysql

I'm attempting to update some values in my database, they are currently empty and I want to add them in increments of 1 starting with 1.
Example
Value
1
2
3
4
5
6
I have tried this but it doesn't work.
UPDATE catalog_product_entity_varchar
SET value = '1' + 1
WHERE attribute_id = 136
Any suggestions?
EDIT: I'm using a MySQL server with phpmyadmin

This is what worked in the end.
SET #i := 1;
UPDATE catalog_product_entity_varchar
SET value = #i:=#i+1
WHERE attribute_id = 136;

Assuming SQL server, you could do this:
http://sqlfiddle.com/#!6/ebbba/4
declare #counter int
set #counter = 0
UPDATE catalog_product_entity_varchar
SET value = #counter,
#counter = #counter + 1
WHERE attribute_id = 136

Try something like this (SQL Server only):
with [incremented] as
(
select attribute_id, row_number() over(order by attribute_id) [no], value
from catalog_product_entity_varchar
)
UPDATE [incremented]
SET value = [no];
Check SqlFiddle

Related

How to split count() query into 10 groups in my sql

I am currently using mysql and I have to split the data into 10 groups.
For example, if the total count of data is 90, it should go like
1~9,
10~18,
19~27,
28~36,
37~45,
46~54,
55~63,
64~72,
73~81,
82~90.
if the total count of data is 100, it should go like
1~10,
11~20,
21~30,
31~40,
41~50,
51~60,
61~70,
71~80,
81~90,
91~100.
Can anyone give me a clue to split the data into 10 groups. I used rownum, but it did not work....
select total.row_num,
total.name,
total.reg,
total.id,
total.motspd
from(
select
(#row_num:=#row_num+1) AS row_num,
cg.group_name as name,
td.reg_date as reg,
td.car_id as id,
td.mcu_motspd as motspd
from
cartracker.tracker_data td
left join car c on (c.car_device_no = td.car_id)
left join car_group cg on (c.car_group_no = cg.car_group_no)
where cg.car_group_no = "1"
group by DATE_FORMAT(td.reg_date, "%Y%M%d%h%m")
)total
This is a result of a query, but it shows wrong row numbers.I want row_num goes from 0 to the end number of the data. but, in the picture, it starts from 44,713. can anyone help me to fix row num as it starts from 0 to the end number of the data.
attached image
I am going by the question in the title:
I am currently using mysql and I have to split the data into 10 groups.
That is exactly what the function ntile() does. So:
select t.*,
ntile(10) over (order by <whatever>) as tile
from t;
I have no idea what the query has to do with this question.
Haven't realize OP want with dynamic table BEFORE re-edit, this is not cleaver and require a lot of time to process though. switch a with OP table name and create rownumber first.
DECLARE #TotalNum INT;
DECLARE #Num INT;
DECLARE #NUm2 INT;
DECLARE #COUNTS INT;
SET #COUNTS = (select count(rownum) from a)/10
SET #TotalNum = 10
SET #Num =1
SET #Num2 =0
WHILE #Num <= #TotalNum
BEGIN
update a
set a.flag = #Num
where a.rownum >= (#COUNTS)*#NUM2+1 and a.rownum <= #NUM*(#COUNTS)
SET #Num = #NUM + 1
SET #Num2 = #NUM2 + 1
END

How to increment using a SQL variable within an update statement

I am trying to increment a column using an #count variable in SQL. I have tried multiple attempts that I will list below that all result in:
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ...
First was:
SET #count = 65;
UPDATE table t
SET t.Revision = CHAR(#count)
, #count = #count + 1
WHERE t.hidden = 0;
I am trying to increment every row currently as a proof of concept that this works.
Second was:
DECLARE t CURSOR FOR
SELECT * FROM table
WHERE t.hidden = 0;
OPEN t;
FETCH NEXT FROM t;
WHILE ##FETCH_STATUS = 0
BEGIN
UPDATE table t2 SET t2.Revision = 'D' WHERE t2.id1 = t.id1 AND t2.id2 = t.id2;
END;
END
CLOSE t;
DEALLOCATE t;
Once again I am just trying to see if I can set a standard variable using a while loop before I implement incrementing as a proof of concept that it works.
I am not sure why either of these attempts is failing but any help would be appreciated.
Here is how your first example should work(inside of some loop):
first you set your count value, then you update
SET #count = 65;
UPDATE CUSTOMER t
SET t.LName = CONVERT(#count, char)
where t.FName = 'a';
...and then increase that count and you update again...
set #count = #count + 1;
UPDATE CUSTOMER t
SET t.LName = CONVERT(#count, char)
where t.FName = 'a';
But that should be in a procedure for example.
Here is the DEMO. I it a small example and I hope you will find it helpful.
Cheers!
You can try the following solution:
SET #count = 64; -- so the first increment is 65 (starting on A).
UPDATE table_name t
SET t.Revision = CHAR(#count:=#count+1)
WHERE t.hidden = 0;
or (shorter):
UPDATE table_name t, (SELECT #count:=64) t2
SET t.Revision = CHAR(#count:=#count+1)
WHERE t.Hidden = 0;
demo on dbfiddle.uk

MySQL twice update with one command

In the below Update command i want to set all delay field to 0 and an id of 2 must be update to 1
UPDATE `tsms_entry_exit`
SET `delay`=(
CASE `delay`
WHEN `id` = 2 THEN 1
ELSE 0
END
)
WHERE user_id = 1
Otherwise:
UPDATE `tsms_entry_exit`
SET `delay`=0
WHERE user_id = 1;
UPDATE `tsms_entry_exit`
SET `delay`=0
WHERE user_id = 1 AND id = 2
How do i update twice in one update command?
If I understand very well, might be you want this
UPDATE `tsms_entry_exit`
SET `delay`= CASE WHEN `id` = 2 THEN 1
ELSE 0
END
WHERE user_id = 1
This is how you can use the update command
update tsms_entry_exit set
delay =
case when id = 2 then 1
else 0
end
where user_id = 1
DEMO

Counting with MySQL

Is there a way to count an Int in a MySQL Update Query?
Currently i have
UPDATE mails SET uid = 4275
and i want something like
UPDATE mails SET uid = (4275++)
Do you want to do something like this?
SELECT #i:=425;
UPDATE mails SET uid = #i:=#i+1;
UPDATE mails SET uid = uid + x
x meaning any number
I didn't get your question properly but I am just answering as I understood
UPDATE mails SET uid = uid + 1
i.e to increment current uid by the lastuid + 1
If you need to update the table so that increment each uid with 1 then you can do this:
UPDATE mails
SET uid = uid + 1;
But if you need to increament each value uid by an incremental value try this:
SET #counter = 0;
UPDATE mails m1
INNER JOIN
(
SELECT *, (#counter := #counter +1) as counter
FROM mails
) m2 ON m1.uid = m2.uid
SET m1.uid = m1.uid + m2.counter
And if you want to count from 4275 on, just set the counter to SET #counter = 4275
UPDATE mails SET uid = uid +1 will add 1 to every uid column.
try
UPDATE mails SET uid = uid+1
where uid = 4275
You can keep your own counter
declare #curr_uid integer;
SET #curr_uid = 4275; -- initial uid
update mails
set uid = #curr_uid , #curr_uid = #curr_uid + 1

Changing duplicate rows to a unique value

I have a table (FORM) with a column like:
Serialno
424
536700045
345293885
424
466758884
424
424
244002678
My aim is to change duplicate rows of 424 to 424+(a unique 6 digit number) but the query below ended up changing all occurrence of 424 to 424748793.
i need to make the query generate a unique value for each row with a duplicate of 424 excluding the first 424.
Thanks.
declare #count int
declare #num varchar (9)
declare #id varchar (9)
dcelare #generic varchar(9)
set #id = RIGHT('000000' + CAST(ABS(CHECKSUM(NEWID())) % 999999 AS varchar(6)), 6)
set #num= '424'
set #generic = #id+#num
select #count= COUNT(serialno) from FORM
where serialno = '424'
while #count <> 1
begin
update FORM SET Serialno = #generic WHERE serialno = '424'
set #count = #count-1
end
If you have any other primary key column then you can update your data based on primary key value which is unique for all records. If you have no any other primary key column then you can do it by using temp table and row_number function like below :
SELECT Col1, Col2, Serialno,ROW_NUMBER() OVER(ORDER BY Serialno) AS RowNo INTO #TempTable FROM FormTable
UPDATE #TempTable
SET Serialno = Serialno + RIGHT('000000' + CAST(ABS(CHECKSUM(NEWID())) % 999999 AS varchar(6)), 6)
WHERE RowNo <> 1
DELETE FROM TestTable WHERE Serialno = '424'
INSERT INTO TestTable
SELECT Col1,Col2 Serialno FROM #TempTable
DROP TABLE #TempTable
To reproduce your current logic you would just need to do
UPDATE FORM
SET Serialno = '424' +
RIGHT('000000' + CAST(ABS(CHECKSUM(NEWID())) % 999999 AS VARCHAR(6)), 6)
WHERE serialno = '424'
Of course this doesn't guarantee that all of the newly generated values will be unique or that they won't clash with any pre-existing values in the table.
If your table has no existing 424xxxxxx you can assign sequential serial numbers as follows.
;WITH CTE AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY $/0) AS RN
FROM FORM
WHERE serialno = '424'
)
UPDATE CTE
SET Serialno = '424' + RIGHT('000000' + CAST(RN AS VARCHAR(6)), 6)