How to handle versioning in sql with single query - mysql

Each Lead put data into pre_order_quotation table with a new version each time.
I am trying to get row count of all quotation by a lead & add 1 to it to get the version of new entry of quotation .
Was trying to achieve it in single query
insert into pre_order_quotation (lead_id,version,created_at,updated_at,file_name) values (1405,(select count(*) from pre_order_quotation where lead_id = 1405)+1,'2020-08-22 12:13:51','2020-08-22 12:13:51','dummy-5f410bffbcc80.pdf')
But i am getting the following error :
You can't specify target table 'pre_order_quotation' for update in FROM clause
How can i achieve it in single query?

Bury the increment a bit deeper..
insert into pre_order_quotation (lead_id,version,created_at,updated_at,file_name)
values (1405,
(select cnt from (select count(*) + 1 cnt from pre_order_quotation where lead_id = 1405) s ),
'2020-08-22 12:13:51','2020-08-22 12:13:51','dummy-5f410bffbcc80.pdf');

Related

Get Average and upload into new db table mysql

I am trying to get the average of the columns in my table and then insert the averages into a second table, I have over 30 columns so I would rather not have to do them all individually if possible
command.CommandText = " INSERT INTO FaceAverages(`rightEyeRightUpper`),(`rightEyeLeftUpper`),(`rightEyeRightLower`),(`rightEyeLeftLower`),(`leftEyeRightUpper`)... FROM(SELECT AVG (rightEyeRightUpper),(rightEyeLeftUpper),(rightEyeRightLower),(`rightEyeLeftLower`)... FROM 'FaceDistancesHappy')";
Assuming you table FaceAverages contains the coulmns
`rightEyeRightUpper`,`rightEyeLeftUpper`,`rightEyeRightLower`,
`rightEyeLeftLower`,`leftEyeRightUpper`...
then you could use an insert select as
command.CommandText = " INSERT INTO FaceAverages(`rightEyeRightUpper`,`rightEyeLeftUpper`,
`rightEyeRightLower`, `rightEyeLeftLower`,`leftEyeRightUpper`... )
SELECT AVG(rightEyeRightUpper), AVG(rightEyeLeftUpper),
AVG(rightEyeRightLower), AVG(`rightEyeLeftLower`), ...
FROM 'FaceDistancesHappy'";
declaring all the column you want insert crresponding to all the column you want select . and using the AVG() function for each column in select

MAX(count) + 1 when inserting, MySQL

I currently use the below query to increment the third column count on every insert.
$DB2->query("INSERT INTO relations (item_ID,tag_ID,count)
SELECT '$ID', '$tag_id', MAX(count) + 1
FROM relations
WHERE tag_ID = '$tag_id';");
The problem is when there is no rows in the table and i try to insert, the Max(count) + 1 is just null. I've tried defining the default value as zero but still null. The column should be 1 on first insert.
How do i change the query, so if first insert then count is 1. I don't want to do a select query before because this code is in a loop.
add an ifnull(...,1)
"INSERT INTO relations (item_ID,tag_ID,count)
SELECT '$ID', '$tag_id', ifnull(MAX(count) + 1,1)
FROM relations
WHERE tag_ID = ''$tag_id';");

Why am i getting "Subquery returns more than 1 row"

Hi I am making a webrowser game and I am trying to get monsters into my data base when I get the error:
Subquery returns more then 1 row
here is my code
INSERT INTO monster_stats(monster_id,stat_id,value)
VALUES
( (SELECT id FROM monsters WHERE name = 'Necroborg!'),
(SELECT id FROM stats WHERE short_name = 'atk'),
2);
any ideas how to fix this problem?
Try use LIMIT 1
INSERT INTO monster_stats(monster_id,stat_id,value) VALUES ((SELECT id FROM monsters WHERE name = 'Necroborg!' LIMIT 1),(SELECT id FROM stats WHERE short_name = 'atk' LIMIT 1),2);
Or you could use Insert from select, with join, if you have relations with 2 tables.
INSERT INTO monster_stats(monster_id,stat_id,value)
(SELECT monsters.id, stats.id, 2 as value FROM monsters
LEFT JOIN stats on monsters.id = stats.monsters_id
WHERE monsters.name = 'Necroborg!'
AND stats.short_name = 'atk'
)
MYSQL insert from select:
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
The problem is one or both of the following:
There is more than one monster named 'Necroborg!'.
There is more than on stat named 'atk'.
You need to decide what you want to do. One option (mentioned elsewhere) is to use limit 1 to get only one value from each statement.
A second option is to better specify the where clause so you get only one row from each table.
Another is to insert all combinations. You would do this with insert . . . select and a cross join:
INSERT INTO monster_stats(monster_id, stat_id, value)
SELECT m.id, s.id, 2
FROM (SELECT id FROM monsters WHERE name = 'Necroborg!') m CROSS JOIN
(SELECT id FROM stats WHERE short_name = 'atk');
A third possibility is that there is a field connecting the two tables, such as monster_id. But, based on the names of the tables, I don't think that is true.

mySQL - INSERT query that matches the same records as this SELECT query?

I've got a select query I'm using to pick out contacts in my DB that haven't been spoken to in a while. I'd like to run an INSERT query to enter in a duplicate note for all the records that are returned with this select query... problem is I'm not exactly sure how to do it.
The SELECT query itself is likely a bit of a convoluted mess. I basically want to have the most recent note from each partner selected, then select ONLY partners that haven't got a note from a certain date and back... the SELECT query goes:
SELECT * FROM
(
SELECT * FROM
(
SELECT
partners.partners_id,
partners.CompanyName,
notes.Note,
notes.DateCreated
FROM
notes
JOIN
partners ON notes.partners_id = partners.partners_id
ORDER BY notes.DateCreated DESC
) AS Part1
GROUP BY partners_id
ORDER BY DateCreated ASC
) AS Part2
WHERE
DateCreated <= '2013-01-15'
How would a run an INSERT query that would only go into the same records as this SELECT?
The insert would enter records such as:
INSERT INTO notes
(
notes_id,
partners_id,
Note,
CreatedBy,
DateCreated
)
SELECT
UUID(),
partners.partners_id,
'Duplicated message!',
'User',
'2013-02-14'
FROM
partners
If you want to do this all in SQL, you could use an UPDATE statement.
UPDATE tablename SET note='duplicate' where id in ( your statement here);
Note that in order for this to work 'id' needs to be a column from 'tablename'. Then, your statement has to return a single column, not *. The column returned needs to be the id that will let your update statement know which rows to update in 'tablename'.

MySQL - How can I update a table with values from another table?

I have the task to repair some invalid data in a mysql-database. In one table there are people with a missing date, which should be filled from a second table, if there is a corresponding entry.
TablePeople: ID, MissingDate, ...
TableEvent: ID, people_id, replacementDate, ...
Update TablePeople
set missingdate = (select replacementDate
from TableEvent
where people_id = TablePeople.ID)
where missingdate is null
and (select count(*)
from TableEvent
where people_id = TablePeople.ID) > 0
Certainly doesn't work. Is there any other way with SQL? Or how can I process single rows in mysql to get it done?
We need details about what's not working, but I think you only need to use:
UPDATE TablePeople
SET missingdate = (SELECT MAX(te.replacementDate)
FROM TABLEEVENT te
WHERE te.people_id = TablePeople.id)
WHERE missingdate IS NULL
Notes
MAX is being used to return the latest replacementdate, out of fear of risk that you're getting multiple values from the subquery
If there's no supporting record in TABLEEVENT, it will return null so there's no change