'Add a random value to an existing column in mysql and return it
I am getting a little more involved with mysql and wanted to add a value to an existing value in a column.
I have already written the function for this. Previously I used the following function, but of course this no longer works with mysql, if I have seen correctly:
earnings = random.randrange(300)
My new function is therefore:
await cur.execute("UPDATE economy SET wallet=wallet + ROUND (1 + rand() *300) WHERE user=%s", (ctx.author.id,))
This part also works. But I would like to get the generated value from my ROUND (1 + rand() *300) function and output it. Is this possible at all and if so: How?
About other functions or possibilities that show me a number at the end I would be grateful too!
Posts I have already looked at:
https://database.guide/mysql-rand-function-generate-a-random-number-in-mysql/
Solution 1:[1]
You could do this using a user-defined variable:
SET @r = ROUND(1 + RAND() * 300);
UPDATE economy SET wallet=wallet + @r WHERE user=%s;
SELECT @r;
I'll leave the Python code to execute these statements to you. Just use the same connection, and that will ensure that the user-defined variable will retain its value from one execute() call to the next.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bill Karwin |
