Counting with MySQL - 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

Related

MySQL copy multiple row-entries to another position

I want to copy a big amount of values from a specific row and specific date to the same row, but another date. I have tried it with the SQL UPDATE SET function and two different tables, but the SET will only use the first value and copy it to the right date-columns.
At this picture you see the results:
And this is one of the queries I tried:
UPDATE Test_tab t
SET t.testValue =
(SELECT testValue
FROM Test_tab
WHERE testDate > '2015-10-01' AND testDate < '2015-10-31'
LIMIT 1)
WHERE t.testDate > '2015-11-01' AND t.testDate < '2015-11-31'
you may try like this
CREATE PROCEDURE your_proc()
BEGIN
DECLARE cont INTEGER;
SET cont = 0;
SET max = SELECT count(*) FROM test_tab;
WHILE cont < max DO
UPDATE Test_tab t SET t.testValue =(SELECT testValue FROM Test_tab WHERE testDate >'2015-10-01' AND testDate <'2015-10-31'
LIMIT cont, 1) WHERE t.testDate > '2015-11-01' AND t.testDate < '2015-11-31'
SET cont = cont + 1;
END WHILE;
END;

Updating multiple tables using single trigger

I am having trouble with updating 2 tables with one trigger, its giving me an error near "New.userid"
CREATE TRIGGER userDownloads AFTER INSERT ON Downloads
FOR EACH ROW
UPDATE Project SET PROJECT_DOWNLOADS = PROJECT_DOWNLOADS + 1 WHERE PROJECTID = NEW.ProjectID,
UPDATE User SET NO_OF_DOWNLOADS = NO_OF_DOWNLOADS + 1 WHERE USERID = NEW.UserID;
You have to write your trigger this way:
DELIMITER //
CREATE TRIGGER userDownloads AFTER INSERT ON Downloads
FOR EACH ROW
BEGIN
UPDATE Project SET PROJECT_DOWNLOADS = PROJECT_DOWNLOADS + 1 WHERE PROJECTID = NEW.ProjectID;
UPDATE User SET NO_OF_DOWNLOADS = NO_OF_DOWNLOADS + 1 WHERE USERID = NEW.UserID;
END//
Please see it working here.
But are you sure you need a trigger? Why don't you just do a COUNT on your Downloads table?
SELECT ProjectID, COUNT(*) AS PROJECT_DOWNLOADS
FROM Downloads
GROUP BY ProjectID;
SELECT UserID, COUNT(*) AS NO_OF_DOWNLOADS
FROM Downloads
GROUP BY UserID;

MySQL increment values of 1

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

Update cell value for each record

I want to update a cell value of each record like,
I have user table in which there is an email field which is unique, I want to update all record's email fields.
Something like this:
update user set email='abdullah+00(i)#gmail.com'
How can I achieve this?
UPDATE user
JOIN (SELECT #i := 0) var
SET email = CONCAT('abdullah+', LPAD(#i := #i + 1, 3, '0'), '#gmail.com')
You can use string concat
update user set email=concat('abdullah',LPAD(i,3,'0'),'#gmail.com');
EDiT if i is not a column
update user (SELECT #i := 1) m set email=concat('abdullah',LPAD(#i=#i+1,3,0),'#gmail.com');
Do something like this:
while c<10 do // or the number of fields you have
update user set email = CONCAT('abdullah+00',i,'#gmail.com')
You may try like this:-
update user
(SELECT #i := 1) m
set email=CONCAT('abdullah+' , LPAD(#i := #i + 1, 3, '0') , '#gmail.com')
I assumed that i is a counter

How to simplfy this query?

Query 1:
SET #count = 0;
UPDATE a_daily_copy_copy
SET a_daily_copy_copy.Cummulative_Target = #count:= target + #count
where a_daily_copy_copy.Site_id = 1
and a_daily_copy_copy.Year=4
and a_daily_copy_copy.Billing_cycle=1
ORDER BY date
Query 2: Modified the a_daily_copy_copy.Billing_cycle=2
SET #count = 0;
UPDATE a_daily_copy_copy
SET a_daily_copy_copy.Cummulative_Target = #count:= target + #count
where a_daily_copy_copy.Site_id = 1
and a_daily_copy_copy.Year=4
and a_daily_copy_copy.Billing_cycle=2
ORDER BY date
I'm a beginner and as of now I'm running the query every time manually by editing the query 1 , and I know both queries can be consolidated into a single query.
I tried solving with Group by function but couldnt come up with Please help me.
Have screened the table:
Looks to me that you can just do:
SET #count = 0;
UPDATE a_daily_copy_copy
SET a_daily_copy_copy.Cummulative_Target = #count:= target + #count where a_daily_copy_copy.Site_id = 1 and a_daily_copy_copy.Year=4 and a_daily_copy_copy.Billing_cycle IN (1, 2)
ORDER BY date
...unless I've missed a difference between the two queries other than the billing cycle.