Update cell value for each record - mysql

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

Related

How to add series number value on my sql columns

I have a table and I need to put a series number(without duplicate) on each value of the columns.
I do it manually by editing each columns using this,|
UPDATE mytable where column = '123'
SET table_column1 = '955';
then next
UPDATE mytable where column = '124'
SET table_column2 = '956';
but this one takes time for me.
There's any solution or fastest way to do this??
I'm thinking of I will add another auto increment but 2 increment is advisable ?
Here's the screenshot of my data below
You can't add another auto-increment. But you can do it with one query if you don't care about a specific order
UPDATE mytable
cross join (select #rank := 0) r
SET table_column2 = 956 + (#rank := #rank + 1)
SQLFiddle demo
If you need a specific order then you can define the #rank variable outside the query and add an order by
set #rank := 0;
UPDATE mytable
SET table_column2 = 956 + (#rank := #rank + 1)
order by id
SQLFiddle demo

Dynamically choose which table

I want to have the query dynamically choose which table it looks up against based on a value in a particular row in another table.
I have this query:
SELECT d.name
FROM `database1`.domains AS d
WHERE (SELECT COUNT(u.id) FROM <<d.db_name>>.users u) > 0
I want to use the value of d.db_name as database name.
Example: d.db_name = database2
i want this:
SELECT d.name
FROM `database1`.domains AS d
WHERE (SELECT COUNT(u.id) FROM `database2`.users u) > 0
You could use variables for this:
SET #table_name = "some_table";
SELECT * FROM #table_name;
If you want to change the variable value depending on the results of your select you coudl use IF like this:
IF(some_column>50, #table_name := "value for true", #table_name := "value for false");

Insert sequential numbers based on another field - MySQL

There is a similar question
Insert sequential number in MySQL
I want to insert sequential numbers to the table, but based on another field. I have two columns page_numner and parent, so the rows with same parent should have page_number as consequtive numbers. If parent changes, the page should start from 1 again and increase by one.
I was thinking to use smth like this
SELECT #i:=0;
SELECT #p:=0;
UPDATE my_table AS t SET page_number = CASE
WHEN #p = t.`parent` THEN #i:=#i+1
ELSE 1 -- assign current parent to #p ??
END
but, it cant figure out how to assign the new parent into #p for the else case.
Please note, that I am trying to achieve this with pure mysql (if possible of course)
Thanks
You can do what you want with this code:
set #p := -1;
set #i := 0;
UPDATE my_table t
SET page_number = (CASE WHEN #p = t.`parent` THEN #i := #i+ 1
WHEN (#p := t.parent) = NULL THEN NULL -- never happens
ELSE #i := 1
END)
ORDER BY t.parent;
Unfortunately, MySQL doesn't allow both ORDER BY and JOIN in the same UPDATE query. If it did, you could initialize the variables in the query.
Note the second condition just does the assignment. = NULL never returns TRUE.

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

MySQL incrementing value

Is there a way to make a value increment with every insert if having multiple inserts? (I dont speak of the primary key that autoincrements)
Lets say I have a structure like this:
|ID_PRODUCT|ID_CATEGORY|NAME|POSITION|
So I have individual product ids, each produt belongs to a category and has a different position in this category. I want to do something like this:
INSERT INTO products
( SELECT id_product, id_category, name, MY_POSITION++
FROM db2.products WHERE id_category = xxx )
So there should be a variable MY_POSITION that starts with 1 and increments every insert.
It would be really easy to do this all just with a scripting-language like php or python, but I want to get better with SQL :)
Yes: Use a user defined variable:
SET #position := 0; -- Define a variable
INSERT INTO products
SELECT id_product, id_category, name, (#position := #position + 1)
FROM db2.products
WHERE id_category = xxx;
The result of increment to #position is the value used for the insert.
Edit:
You can skip the declaration of the variable by handling the initial value in-line:
...
SELECT ..., (#position := ifnull(#position, 0) + 1)
...
This can be particularly handy when executing the query using a driver that does not allow multiple commands (separated by semicolons).
You will need to ORDER BY id_category and use two user variables so you can track the previous category id -
SET #position := 0;
SET #prev_cat := 0;
INSERT INTO products
SELECT id_product, id_category, name, position
FROM (
SELECT
id_product,
id_category,
name,
IF(#prev_cat = id_category, #position := #position + 1, #position := 1) AS position,
#prev_cat := id_category
FROM db2.products
ORDER BY id_category ASC, id_product ASC
) AS tmp;
This will allow you to do all categories in one query instead of category by category.
Purely to add to #Bohemians answer - I wanted the counter to reset every so often and this can be done like such:
SELECT *,(#position := IF (#position >= 15,1,#position + 1))
Where 15 is obviously the maximum number before reset.
Try setting a value using a subquery like this
(SELECT MAX(position) FROM products AS T2)+1
Or
(SELECT MAX(position) FROM products AS T2 WHERE id_category = 'product category')+1
Hope this will work.
update <tbl_name> set <column_name>=<column_name>+1 where <unique_column/s>='1'";
For those who are looking for example of update query, here it is:
SET #i := 0;
UPDATE products SET id = (#i := #i + 1);