So I have the code for generating the kind of passwords I want which is
SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 6) AS Paswoord
How do I make this work for all the 60 rows I already have at once.
I think it might be with Inner Join etc. I have tried some stuff but the all fail.
Thanks for reading.
UPDATE USER SET
PASSWORD = SUBSTRING(MD5(RAND(ID)) FROM 1 FOR 6)
WHERE PASSWORD IS NULL -- or whatever consition matches rows you want to update
Try this please: sorry made a typo - anyway the above two answers are there to help you :)
UPDATE maintable
SET maintable.column = (SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 6))
UPDATE tableName
SET columnName = SUBSTRING(MD5(RAND()),1,6)
Related
I have a long list of data and i would want to copy data from three columns and combine them in one column as in the image below
So that the result would be like in the table below:
Any answer mysql would be highly appreciated
i.e. UPDATE location SET ... something like that
Try the following query:
Query#1
UPDATE location SET more = CONCAT(county,' ',constituency,' ',ward);
Note:
If county OR constituency OR ward column contains NULL THEN you may check null before updating:
Query#2
UPDATE location
SET more = CONCAT( IFNULL(county, ''),' ',IFNULL(constituency, ''),' ',IFNULL(ward,''));
Why should you go for Query#2
You may get an idea about the reason behind using IFNULL:
SELECT CONCAT('ABCD',null,'EFGH');
Result: NULL
SELECT CONCAT('ABCD',IFNULL(null,''),'EFGH')
Result: ABCDEFGH
Caution:
CONCAT_WS might encounter data loss. The following example is a good indication:
SELECT CONCAT_WS('ABCD',null,'EFGH')
Result: EFGH. (You just lost ABCD)
Try this.
UPDATE location SET more = CONCAT_WS(' ',county,constituency,ward);
You can use CONACT() method of mysql like this:
UPDATE your_table SET d = CONCAT CONCAT(a ,' ' ,b ,' ' ,c);
How can I use a result of select in update query? For example:
Update access_rights
set rfidcode=(Select rfidcode from users where name like 'thomas')
where id_access_rights=3;
This doesn't work. Can anyone help me ?
assuming you want to update a single record your select query needs to return a single result. use the limit keyword.
Update access_rights
set rfidcode=(Select rfidcode from users where name like 'thomas' limit 1)
where id_access_rights=3;
vkp solution if you want to update many related records but you have to watch your joins or you'll get errors, or worse corrupt your data
Update access_rights a, users u
set a.rfidcode = u.rfidcode
where a.userid = u.userid --change this to appropriate join column
and u.name like '%thomas%'
and a.id_access_rights=3;
I'm trying to make an query that checks if the field 'field_A' in 'Table_A' is '0' and if it is, update 'field_B' in 'Table_B' to '10'.
I have no idea if this is even possible. I have very little experience with mysql and I'm just trying some things out. But if it is, I'd like to know how this is done.
update table_B
set field_B = 10
where exists (select 1 from table_A where field_A = 0)
For the following table (all columns are integers)
[id, value, best_value]
For a given id and value I want update it's row setting the best_value column to max(newvalue,best_value). I seached into the documentation but I dont see a function for doing so.
Thanks
You want GREATEST(x,y). Example, if the new value is 530:
UPDATE my_table SET best_value = GREATEST(530,best_value) WHERE id=123
You don't strictly need any such function,
UPDATE my_table SET best_value = new_value
WHERE id=123 AND best_value < new_value
would do the job about as well as AlienWebguy's answer :)
I have an access DB which we use to track tickets. Each ticket may have multiple occurrences because of different programming changes associated with that ticket. Each record also has a program_type field which is SVR or VB. Example:
123456 - SVR - SomeCode
123456 - VB - SomeVBCode
I've added a column to the database called VB_Flag, which defaults to 0, which I would like to change to the number 1 for every ticket containing VB code. So, the result here would be:
123456 - SVR - SomeCode - 1
123456 - VB - SomeVBCode - 1
But, I can't figure out for the life of me how to write this update query. At first I tried:
UPDATE table SET VB_Flag = 1 WHERE program_type = 'VB'
But that obviously left out all the SVR code that shared a ticket number with the VB code.
I'm at a loss. Help?
You can do something like this:
UPDATE tickets SET VB_Flag = 1
WHERE ticket_id IN (SELECT ticket_id FROM tickets WHERE program_type = 'VB');
The inner SELECT statement returns a list of all ticket_ids that have a program_type of 'VB'.
The update then sets the VB_Flag to 1 for ALL records with one of those ticket_ids (that includes those with a program_type of 'SVR'.
This should work:
UPDATE table SET VB_Flag = 1
WHERE TicketNum IN (SELECT TicketNum FROM Table WHERE program_type = 'VB')
UPDATE
Table
INNER JOIN Table AS Table2
ON Table.TicketNumber = Table2.TicketNumber
SET
Table2.VB_Flag = 1
WHERE
(([Table].[program_type]="VB"))
A simple nested select should take care of your problem:
UPDATE myTable
SET VB_Flag = 1
WHERE TicketID in (Select TicketID from myTable WHERE program_type = 'VB')