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);
Related
I have a table called employeepostinghistory with the following columns:
employee_posting_id, employee_posting_to, employee_posting_from, emp_emp_cnic
employee_posting_id is primary key and emp_emp_cnic is foreign key
Basically this table is responsible to hold employee history with from and to dates i.e. employee_posting_from and employee_posting_to.
I want to update all records setting employee_posting_to=NULL Where employee history is latest so, I have used DESC on employee_posting_from. But, UPDATE query says subquery return more than 1 record, What can be the possible solution for this problem.
UPDATE employeepostinghistory
SET employeepostinghistory.posting_to=NULL
WHERE employeepostinghistory.emp_emp_cnic=(
SELECT DISTINCT employeepostinghistory.emp_emp_cnic
from employeepostinghistory
GROUP BY employeepostinghistory.emp_emp_cnic
ORDER BY employeepostinghistory.posting_from DESC
)
;
UPDATE employeepostinghistory
NATURAL JOIN (
SELECT emp_emp_cnic, MAX(datetime_column) datetime_column
FROM employeepostinghistory
GROUP BY 1
) last_row_per_employee
SET employeepostinghistory.posting_to=NULL
;
emp_emp_cnic.datetime_column must be unique.
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.
I have inherited a table with a field "sku" with should be unique, but thanks to a failing sku-generating method is now littered with dozens of duplicates all around.
I need to quickly fix these duplicates (other parts of the application are failing when encountering these duplicate records) by running an update and appending the record ID to the SKU (which is a valid solution for the time being for this application).
I'm trying to run:
UPDATE
main_product_table
SET sku = CONCAT(sku, '-', CAST(product_id as CHAR) )
WHERE sku IN (
SELECT sku FROM main_product_table
GROUP BY sku
HAVING COUNT(*) > 1
);
But I receive:
You can't specify target table 'main_product_table' for update in FROM clause
Is there a way to accomplish the same? Is mysql complaining about me having main_product_table both in the update and in the subquery to get the duplicates?
Thanks!
Try this:
UPDATE
main_product_table
SET sku = CONCAT(sku, '-', CAST(product_id as CHAR) )
WHERE sku IN (
select * from ( SELECT sku FROM main_product_table
GROUP BY sku
HAVING COUNT(*) > 1) as p
);
Added table alias in inner query.
I have this table
What I want to do is that Select the attr_id WHERE a DISTINCT name_en-GB appears and then SET that selected attr_id for that name_en-GB
I can do this by writing individual queries but I want to know is there any way I can do this in one query?
I have tried this
UPDATE sc_product_phrase
SET attr_id = (
SELECT DISTINCT
sc_product_phrase.caption,
sc_product_phrase.design_phrase_id
FROM
`sc_product_phrase` as x
GROUP BY
sc_product_phrase.caption
)
but this show error
[Err] 1093 - You can't specify target table 'sc_product_phrase' for
update in FROM clause
EDITED
I want my TABLE to look like the following http://sqlfiddle.com/#!2/d65eb/1
NOTE:- I dont want to use that query in that fiddle
Your SQL Fiddle makes the question much clearer. You want the minimum attribute id on all rows with the same value in the last column. You can do this with an update/join like this:
UPDATE table1 JOIN
(SELECT `name_en-GB`, min(Attr_Id) as minai
from table1
GROUP BY `name_en-GB`
) tt
on table1.`name_en-GB` = tt.`name_en-GB`
SET attr_id = tt.minai
WHERE table1.`name_en-GB` IN ('Bride Name', 'Child Grade') AND
table1.attr_id <> tt.minai;
I'm not sure if you need the in part. You can update all of them by removing that clause.
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'.