MySQL - Update joined column with random values of last 12 digits - mysql

I have a MYSQL column called Joined (varchar(50)) with values such as 13 numbers
How can I implement that query ?
UPDATE `users` SET `joined` = 153ZZZZZZZZZZ

Try:
UPDATE `users`
SET `joined` = CONCAT('153', FLOOR(RAND()* 9000000000) + 1000000000)

you could generated md() random string eg:
UPDATE `users`
SET `joined` = concat('153',SUBSTRING(MD5(RAND()) FROM 1 FOR 10) )
or
UPDATE `users`
SET `joined` = concat('153',SUBSTRING(MD5(RAND()) FROM 1 FOR 6),
SUBSTRING(MD5(RAND()) FROM 1 FOR 4) )
or if you need only digit you could use
UPDATE `users`
SET `joined` = concat('153',CAST(rand()*1000000 as UNSIGNED),
CAST(rand()*10000 as UNSIGNED) )
of for rows with short string you could use case when for adapt the code as you need
UPDATE `users`
SET `joined` = case when length(joined)= 14 then
concat('153',CAST(rand()*10000000 as UNSIGNED),
CAST(rand()*10000 as UNSIGNED) )
when length(joined)= 13 then
concat('153',CAST(rand()*1000000 as UNSIGNED),
CAST(rand()*10000 as UNSIGNED) )
.....
end

Related

MySQL Update if value is x or y

I have following procedures which help me to update the dates in my table on a daily basis, keeping them relevant for demo purposes. The difference between those two procedures is only that they are being executed for 2 different clients with property_id being 2 and 3 respectively.
UPDATE `opera_pms_detail` SET `arrival` = SUBDATE(curdate(), 2), `departure` = SUBDATE(curdate(), 1), `read_on` = NULL WHERE `property_id` = 2 AND `import_type` = '1'
UPDATE `opera_pms_detail` SET `arrival` = SUBDATE(curdate(), 2), `departure` = SUBDATE(curdate(), 1), `read_on` = NULL WHERE `property_id` = 3 AND `import_type` = '1'
Now, for efficiency's sake, is there a better way of doing this? Instead of having to call the procedure twice, can I just update the columns for both clients in one call?
Thank you
Try implementing your procedure like this
PROCEDURE ýour_procedure_name(IN p_property_id INT)
BEGIN
UPDATE `opera_pms_detail`
SET `arrival` = SUBDATE(curdate(), 2),
`departure` = SUBDATE(curdate(), 1),
`read_on` = NULL
WHERE `property_id` = p_property_id
AND `import_type` = '1';
END
And you will call your procedure as:
CALL `your_procedure_name`(property_id)

mysql weighted random results - how to get changed variable value after SELECT

I have a table keywords with columns keyword and weight. My goal is to randomly select one keyword, but to regard its weight (probability). I found two ways to solve this, where the latter one is more elegant (and consumes less ressources) - but i dont get it to run. See yourself.
The table and records:
CREATE TABLE IF NOT EXISTS `keywords` (
`keyword` varchar(100) COLLATE utf8_bin NOT NULL,
`weight` int(11) NOT NULL,
UNIQUE KEY `keywords` (`keyword`),
KEY `rate` (`weight`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
INSERT INTO `keywords` (`keyword`, `weight`) VALUES
('google', 50),
('microsoft', 20),
('apple', 10),
('yahoo', 5),
('bing', 5),
('xing', 5),
('cool', 5);
Query 1
Consumes more ressources, i work on 5k+ records. Source is Why would this MySQL query using rand() return no results about a third of the time?:
SELECT * FROM `keywords` ORDER BY -LOG(1.0 - RAND()) / weight LIMIT 1
Query 2
Sums up weights to #weight_sum. Sets #weight_point to RAND() number from within that range. Loops through all records, substracting weight from #weight_pos and setting #keyword to the current keywords.keyword. Until #weight_pos < 0. Then it keeps that keyword. Source is Random Weighted Choice in T-SQL
SET #keyword = 0;
SET #weight_sum = (SELECT SUM(weight) FROM keywords);
SET #rand = RAND();
SET #weight_point = ROUND(((#weight_sum - 1) * #rand + 1), 0);
SET #weight_pos = #weight_point;
SELECT
keyword,
weight,
#keyword:=CASE
WHEN #weight_pos < 0 THEN #keyword
ELSE keyword
END AS test,
(#weight_pos:=(#weight_pos - weight)) AS curr_weight,
#weight_point,
#keyword,
#weight_pos,
#rand,
#weight_sum
FROM
keywords;
See phpmyadmin results here http://postimg.org/image/stgpd776f/
My Question
How do i get the value in #keyword, or what the test column holds in the end? Adding a SELECT #keyword afterwards doesn't change anything.
Ok, i think my question was more or less a basic mysql-question. I achieved what i wanted by encapsulating the above SELECT statement into another SELECT, that then filtered the first one's result for what i searched. Sorry for bothering you. See the query:
SET #keyword = 0;
SET #weight_sum = (SELECT SUM(weight) FROM keywords);
SET #rand = RAND();
SET #weight_point = ROUND(((#weight_sum - 1) * #rand + 1), 0);
SET #weight_pos = #weight_point;
SELECT t.test FROM (
SELECT
keyword,
weight,
#keyword:=CASE
WHEN #weight_pos < 0 THEN #keyword
ELSE keyword
END AS test,
(#weight_pos:=(#weight_pos - weight)) AS curr_weight,
#weight_point,
##keyword,
#weight_pos,
#rand,
#weight_sum
FROM
keywords
) AS t
WHERE
t.curr_weight < 0
LIMIT
1;

INSERT VALUES into empty column

$querychange =
mysql_query("UPDATE table SET clounm='$newvale' WHERE email='$email'")
but put $newvalue into the nearest empty cell
Consider the following code:
UPDATE `table_name` SET `col_name` = `col_value` WHERE `record_name` = 'ID'
for example
UPDATE `student` SET `marks` = 50 WHERE `name` = 'Jack'
This will set all records with name Jack to 50 marks.
Is this what your looking for
UPDATE table SET clounm='$newvale' WHERE email IS NULL LIMIT 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)

Addition in MySQL trigger not working as expected

I'm doing some addition as part of a mysql trigger. The added value is added a to a column on the table. The code is as follows;
BEGIN
IF NEW.Status = 'processed' AND NEW.Success = 1 THEN
INSERT INTO crm_master
(msisdn, last_action_date, source, contract_type, revenue,inc)
VALUES
(new.msisdn,NOW(), 'INC5', new.Contract_Type, revenue = revenue+5, 1)
ON DUPLICATE KEY UPDATE last_action_date = NOW(),
contract_type = new.Contract_Type,
revenue = revenue+5,
inc = 1;
END IF;
END
The column revenue in the table crm_master is set to default of 0
The problem is that I'm getting unexpected results with incorrect values and in some cases 0 even though there should never be a 0 value.
I don't think it's valid reference to default value revenue = revenue+5 in your insert statement. It should look like
INSERT INTO crm_master
(msisdn, last_action_date, source, contract_type, revenue,inc)
VALUES
(new.msisdn,NOW(), 'INC5', new.Contract_Type, DEFAULT(revenue) +5, 1)
ON DUPLICATE KEY UPDATE ....
Or you can simply do
INSERT INTO ....
VALUES
(new.msisdn,NOW(), 'INC5', new.Contract_Type, 5, 1) ...
*Your update part of INSERT ... ON DUPLICATE KEY UPDATE is ok.
INSERT INTO sometable( column ) VALUES ( column = column + 5 );
is equivalent to
INSERT INTO sometable( column ) VALUES ( 0 );
because (column = column+5) evaluates to 0 as column is never equal to column+5.
This syntax is ok for UPDATE query, but for INSERT you should provide an explicit value, like
INSERT INTO sometable( id, column ) VALUES ( 1, 5 )
ON DUPLICATE KEY UPDATE column = column + 5;
which would insert value 5 if there is no row with given id and add 5 to column if there is one.