In MySQL how to calculate rate based on id and value column - mysql

For example I have a table MyTable like this:
Id Value Rate
1 10
2 30
3 40
4 60
5 100
Rate is defined as: Rate = [ Value(id) - Value(5) ] / [ 5 - id ] , for id from 1 to 4.
I'm thinking of doing this:
INSERT INTO MyTable (Id, Rate)
SELECT Id,
??? real work goes here
FROM MyTable
LIMIT 4
ON DUPLICATE KEY UPDATE Rate=VALUES(Rate);
But can someone help me how to do the "rate" part? Thanks!
Another thing, if I define Id like this:
Id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
Is it possible the program could assign the value of Id not continuous, for example, the values of Id like this: 1, 2, 4, 5, 6? I'm asking because if this is possible, my program would fail because I assume the Id values are continuous.
In addition, is it possible the value of Id not starting from 1? For example, the values of Id like this: 2, 3, 4, 5, 6? I'm asking because if this is possible, my program would fail because I assume the Id value always starts from 1 and if there are five rows the last one would be 5.
Thanks for helping!

You can write:
UPDATE MyTable
SET Rate =
( Value - (SELECT Value From (SELECT Value FROM MyTable WHERE id = 5) AS t) )
/ (5 - id)
WHERE id BETWEEN 1 AND 4
;

Related

Starting from unique and progressive `ID` to build a new string using MySQL

Starting from a MySQL table with the following unique and progressive IDs
ID
pID
1
10
100
1000
I need update the column pID of the same MySQL table by constructing a string of type YYYY-ID
Expected Output:
ID
pID
1
2022-0001
10
2022-0010
100
2022-0100
1000
2022-1000
Any help really appreciated.
You can CONCAT '2022-' string to your ID column, padded with LPAD, filled with 0 on the left:
UPDATE table SET pID = CONCAT('2022-', LPAD(ID, 4, 0))
If you need current year:
UPDATE table SET pID = CONCAT(DATE_FORMAT(CURDATE(), '%Y'), '-', LPAD(ID, 4, 0))

Show records where a value exist in all instances of a field by group

I am trying to figure out a way to show all records in table where a specific field does not contain certain values - table layout is:
id
tenant_id
request_action
request_id
request_status
hash
Each request_id could have multiple actions so it could look like:
1 1 email 1234 1 ffffd9b00cf893297ab737243c2b921c
2 1 email 1234 0 ffffd9b00cf893297ab737243c2b921c
3 1 email 1234 0 ffffd9b00cf893297ab737243c2b921c
4 1 email 1235 1 a50ee458c9878190c24cdf218c4ac904
5 1 email 1235 1 a50ee458c9878190c24cdf218c4ac904
6 1 email 1235 1 a50ee458c9878190c24cdf218c4ac904
7 1 email 1236 1 58c2869bc4cc38acc03038c7bef14023
8 1 email 1236 2 58c2869bc4cc38acc03038c7bef14023
9 1 email 1236 2 58c2869bc4cc38acc03038c7bef14023
Request_id can either be 0 (pending), 1 (sent) or 2 (failed) - I want to find all hashes where all the request_status within that hash are set to 1.
In the above two examples a50ee458c9878190c24cdf218c4ac904 should return as a match as all the request_status are 1 but ffffd9b00cf893297ab737243c2b921c should not as, whilst it contains a 1, it also contains some 0's and 58c2869bc4cc38acc03038c7bef14023 should not as, again whilst it contains a 1, it also contains some 2's
I tried:
SELECT
*
from
table
where request_action='email' and request_status!=0 and request_status!=2
group by hash
However, this doesn't give me the result I need - how can I return the hashes only where request_status is set to 1 for all the instances of that hash?
Not sure why you would need a group by here. You'd want to do a group by if you were going to concat data using GROUP_CONCAT, or other aggregate functions (sum, max, etc)
Also, instead of doing multiple negative conditions in your where clause (request_status !=0 and request_status !=2), why not just get the status you want?
SELECT * FROM test WHERE request_action = 'email' AND request_status = 1
Update Based on Your Comment
If you don't want to return any hashes that have the status of 0, or 2. You can do this:
SELECT
*
FROM
test t
WHERE
request_action = 'email' AND request_status = 1
AND HASH NOT IN (SELECT HASH FROM test WHERE request_status IN (0, 2))
Just make sure you have an index on hash, otherwise this is going to be really slow.
Create table temp select hash from your_table where
request_status=1 group by hash
Alter table temp add index(hash)
Delete from temp where hash IN (select hash from temp
where request_status!=1 group by hash)
Select * from your_table where hash IN(select hash from
temp)

Query which Find string and increment the count

I have table like that,
id name count
1 rrr 2
2 www 3
3 qqq 4
4 aaa 5
5 gyhhh 4
6 dfgdfg 5
I want to write the query which find the name in table and if it find then increment the count in count column for that name. The count maintain the no of time name used by the user.If user used the name , then I am check the name in db , if it found then I want to update row with increment in count.
A simple update query required:
If you want to increase count only if the input parameter exactly matches the name then use this:
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` = ?
And if you want to increase count if the input parameter is a substring of name then you can use LIKE
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` LIKE CONCAT('%',?,'%')
Note: Replace the question mark (?) by your input parameter.
Try this:
select id,name, id + 1 from
(Select id,name from table_name where name in('sa','da','ba','ca')) as a;
hope it helps..

Substring in mysql. Selecting some data

I have a table like
ID Value
A2424 1
A5355 2
A6363 3
A4634 4
AA_A2424 5
AA_A6363 6
I would like to select only those ID's that show up after AA_
so my output should be
ID Value
AA2424 1
AA5355 2
I tried this but it isn't giving me the right output
SELECT *
FROM table
WHERE ID LIKE SUBSTRING( 'AA_', 4, 8 )
Can anyout suggest??
Thanks
Try:
SELECT ID
, Value
FROM table
WHERE SUBSTRING(ID, 1, 3) LIKE 'AA_%'
I guess your output isn't correct for this example table. It should be:
ID Value
A2424 1
A6363 3
Here is a query to get what you need:
select * from AATable where id in
(select substring(id,4,100) from AATable where id like 'AA_%')

mysql update between row and shift current to right

how to update column value of specific id and shift after to right.
id track
1 3
2 5
3 8
4 9
want to update id 3 track column value to 10, result like this
id track
1 3
2 5
3 10
4 8
5 9
id column is auto_increment
or any suggestion it's my pleasure.
thank you.
You should avoid tweaking auto_increments. Auto increment keys are usually supposed to be used internally (e.g. for linking purposes). If you want to order tracks, i suggest you add a seperate numeric field "ordernro" to the table and update that
To add a column order nro to a table named album, do like this:
alter table album add ordernro int(2) after id;
Then copy the current value for id into this new column:
update album set ordernro=id;
(do this only once after adding the column)
To insert track 10 at position 3 first shift the rows:
update album set ordernro = ordernro + 1 where ordernro >= 3;
And then insert track 10:
insert into album (ordernro, track) values (3, 10);
Remember to update your existing insert/update/select statements accordingly.
The result can be checked by:
select * from album order by ordernro;
(The id will now be "mixed up", but that doesn't matter)
UPDATE table SET id = id + 1 WHERE id >= x;
x being the id where you place your current track.
The problem with JK 's answer is that MySQL returns error saying that is can't UPDATE because the index at x+1 would be duplicate.
What I did is
UPDATE table SET id = id + 100 WHERE id >= x;
UPDATE table SET id = id - 99 WHERE id >= x;
And then INSERT my row at index x