I want update query after select query.
first SELECT query , SELECT * from be_settings order by seq desc limit 1
this select query is based on seq, retrieve the last inserted record.
I want update on this record.
second Update Query, UPDATE be_settings set appgubun ='CCTV', running ='on'.
how to update query after select query?
thanks.
You can do this via an update with a subquery:
UPDATE be_settings
SET appgubun = 'CCTV', running = 'on'
WHERE seq = (SELECT t.max_seq FROM (SELECT MAX(seq) AS max_seq FROM be_settings) t );
The subquery in the WHERE clause is necessary because it involves the be_settings table, which is the target of the update. The following would give an error:
WHERE seq = (SELECT MAX(seq) FROM be_settings)
You need to do
UPDATE be_settings set appgubun ='CCTV', running ='on' where seq= '4'
Assuming your there is seq column on be_settings and it is primary key.
and 4 is the last inserted id you are getting from select query.
if you are getting that seq in any variable based on programming language then you have to use that particular variable.
You have to this if you do not want to use subquery.
Related
Below is a MySQL query:
SELECT
COUNT(*) AS counted, employer_group
FROM
employer_survey
GROUP BY employer_group
HAVING counted > 1;
Before I alter table definition for employer_group to unique, I need to create an UPDATE statement to CONCAT() the value of created_dt to employer_group so the alter table will not fail because of values.
How do I do this? I am unable to return id column because I am using GROUP BY and HAVING.
I should mention that I want the id column returned so I may use the above SELECT with an IN clause in my UPDATE statement. This may not be the best approach.
You can do this with join:
update employer_survey es join
(select es2.employer_group
from employer_survey es2
group by es2.employer_group
having count(*) > 1
) eg
on es.employer_group = eg.employer_group
set es.employer_group = concat_ws(' ', es.employer_group, es.created_dt);
I'm having trouble with this one MySQL query -
UPDATE users
SET field = 1
WHERE time BETWEEN '_time1_' and '_time2_'
AND count(email) > 1;
It says I have an error using a group-by function at count(). I've tried HAVING COUNT also. I'm not sure how to specify this query, the actual answer might be more nested than I figured.
count(column) seems to work with SELECT but not with UPDATE.
Assuming that you want to update records with more than one occurrence within time1 and time2, this should do the trick:
UPDATE users
SET field = 1
WHERE
time BETWEEN '_time1_' AND '_time2_'
AND (
SELECT COUNT(email)
FROM users AS ucount
WHERE ucount.time BETWEEN '_time1_' AND '_time2_'
AND users.email = ucount.email
GROUP BY ucount.email
) > 1
But REMEMBER: always back up your data before performing risky bulk updates or do it within a transaction and make sure you got that right before committing.
You can also check what you'll update by doing so:
SELECT * -- Just replace 'UPDATE' for 'SELECT * FROM'
FROM users
-- SET field = 1 -- And comment 'SET' to perform the SELECT
WHERE
time BETWEEN '_time1_' AND '_time2_'
AND (
SELECT COUNT(email)
FROM users AS ucount
WHERE ucount.time BETWEEN '_time1_' AND '_time2_'
AND users.email = ucount.email
GROUP BY ucount.email
) > 1
The above query will return you all the records that will be updated if you run the UPDATE 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'.
I got two MySQL working fine and i'm trying to find a way to combine them into one single query.
First, it selects ID of an employee.
SELECT 'ID' FROM `employee` ORDER BY ID DESC LIMIT 1;
Let's say it returns ID 100;
Then update data of employees whose ID is 100
UPDATE 'LOG' SET `TIME_EXIT`='2013/02/22' WHERE `ID`='100';
Can i do it all in a single query?
Just add them together:
UPDATE LOG SET TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM employee
ORDER BY ID DESC
LIMIT
);
But based on that code currently it'll only ever update the last employee, you will need to select the correct employee by using some other identifier to ensure you have the correct one.
UPDATE LOG SET TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM employee
WHERE NAME = 'JOHN SMITH'
ORDER BY ID DESC
LIMIT 1
);
It's now a few months old, but maybe helps you or others finding this via google…
If you want to UPDATE a field in the same selected table use this:
UPDATE LOG SET
TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM (
SELECT ID
FROM LOG
WHERE whatEverYouWantToCheck = whateverYouNeed
) AS innerResult
)
So, you SELECT id from a subselect. If you try to subselect it directly, mySQL quites with your error message You can't specify target table 'log' for update in FROM clause, but this way you hide your subsubquery in a subquery and that seems to be fine. Don't forget the AS innerResult to avoid getting the error message #1248 - Every derived table must have its own alias. Also match the subsubquery field name to the subquery field name in case you do something like SELECT COUNT(*) or SELECT CONCAT('#', ID)
I want to update multiple rows based on a SELECT sql query.
I want to do it ALL IN AN SQL SHELL!
Here is my select:
SELECT #myid := id, #mytitle := title
FROM event
WHERE pid>0 GROUP BY title
ORDER BY start;
Then, I want to do an update with this pseudocode:
foreach($mytitle as $t)
BEGIN
UPDATE event
SET pid=$myid
WHERE title=$t;
END
But I don't know how to ake a loop in SQL.
Maybe there's a way to make it in a single sql query?
I DON'T WANT ANY PHP!!! ONLY SQL SHELL CODE!!!
I want to update every rows with a pid with the id of the first occurence of an event. Start is a timestamp
I think this should do what you want, but if it doesn't (I'm not sure about joining a subquery in an UPDATE query) then you can use a temporary table instead.
UPDATE
event
JOIN (
SELECT
MIN(pid) AS minPID,
title
FROM
event
WHERE
pid > 0
GROUP BY
title
) AS findPIDsQuery ON event.title = findPIDsQuery.title
SET
event.pid = findPIDsQuery.minPID
Pure SQL doesn't really have "loops", per se: it's a set-based descriptive language. I believe the following update will do what you want (though your problem statements leaves much to be desired—we know nothing about the underlying schema).
update event t
set pid = ( select min(id)
from event x
where x.title = t.title
and x.pid > 0
group by x.title
having count(*) > 1
)
Cheers!