Suppose I have this query:
UPDATE customerDetails SET age = 17
WHERE customerid = (SELECT max(customerid));
Which is meant to change the customer age to 17 for the highest customerid. But, all customer's age change to this value, regardless of ID, why is this?
The result of:
(SELECT max(customerid))
is not the maximum customerid of the table since you did not include a FROM clause.
What is odd is that the code runs without any errors and as it seems this SELECT returns the customerid of each row!!!
But even if you included the FROM clause the code would result in the error:
Error: ER_UPDATE_TABLE_USED: You can't specify target table
'customerDetails ' for update in FROM clause
Mysql allows the subquery only if it is nested inside another one like this:
UPDATE customerDetails
SET age = 17
WHERE customerid = (SELECT t.maxid FROM (SELECT max(customerid) maxid FROM customerDetails) t);
You can do it like this:
UPDATE customerDetails
SET age = 17
ORDER BY customerid DESC
LIMIT 1
What this does is selecting all the data, then order's it by customerid in descending order(that means the first one will be the one with the largest customerid) and then takes the first result with LIMIT 1 and makes an update to that row.
Here is a small DEMO
One more way to do it is:
with cte as(
select max(customerid) as id
from customerDetails
)
update customerDetails c
set age = 17
where c.customerid = (select id from cte);
The query doesn't make sense. You are saying WHERE customerid = (SELECT max(customerid))
This would get the max id for each row individually. If you want just the max id from the entire table, you need to check the entire table again. You can do this by doing it this way:
UPDATE customerDetails SET age = 17
WHERE customerid = (SELECT max(c.customerid) FROM customerDetails c);
UPDATE customerDetails SET age = 17
WHERE customerid = (SELECT max(customerid));
What is happening here? The update statement goes through the table row by row and checks whether the row matches the select clause. In the subquery there is no from clause, so you are not selecting from some table there. Instead you access the customerid of the row you are currently looking at (it is the only customerid that exists in that context). This is one value (one row, one column) and the maximum of that value is the value itself. So, what you have is:
UPDATE customerDetails SET age = 17
WHERE customerid = customerid;
The condition customerid = customerid is true for every row in the table and you update all rows hence.
What you want instead is to update the row the customerid of which matches the maximum customerid in the table:
UPDATE customerDetails SET age = 17
WHERE customerid = (SELECT MAX(customerid) FROM customerDetails);
MySQL, however, has problems with accessing the same table that you are updating. So you will have to select from the table one level deeper, which makes this a tad clumsy:
UPDATE customerDetails SET age = 17
WHERE customerid = (SELECT MAX(customerid) FROM (SELECT * FROM customerDetails) q);
The following should work:
create table temp as select max(customerid) max_id from customerDetails;
UPDATE customerDetails SET age = 17
WHERE customerid = (SELECT max_id from temp);
Related
I have a one query for select ids from table and another query for update the table values. These are the queries used.
for select ids
select id from table_1 where orderId = 41 AND status = 1
for update
UPDATE table_1 SET orderId = '17' WHERE id IN (1,2,3,4,5,6,.....,n);
This two queries working properly. But when try to this code its not working. I want to update orderId update 41 to 17 in all the records selected
UPDATE table_1 SET orderId = '17' WHERE id IN (select id from table_1 where orderId = 41 AND status = 1 );
This query is returns error. Any suggestion to correct this error.
Thank You
The problem is that MySQL does not allow you to use the same table in subqueries in an update or delete. I think you can simplify the logic, so the subquery is not needed.
Why not just use this?
UPDATE table_1
SET order_id = '17'
WHERE order_id = 41 AND status = 1;
Note: If order_id is a number, use 17 not '17' -- don't mix data types.
This assumes that id is unique.
Alternatively, if you really need list of ids, you can also use a JOIN:
UPDATE table_1 t1 JOIN
(SELECT tt1.*
FROM table_1 tt1
WHERE tt1.order_id = 41 AND tt1.status = 1
) tt1
ON tt1.id = t1.id
SET t1.order_id = 17;
student(CNE,NameE,Age)
Delete all students who have a minimal age
is this true ?
delete from student where CNE in(select CNE,MIN(Age) from student);
In MySql you can't access directly the table from which you want to delete rows in a subquery in the WHERE clause.
So nest the subquery that gets the minimum age inside another subquery:
delete from student
where age = (
select t.minage from (
select min(age) minage from student
) t
)
I'm beginning in SQL and don't know how to do that.
What I am looking for is a way to give the same ID to column which as an identical value.
This is my table :
I want 'afrique du sud' for exemple to have the same 'id_country'
How can I do that ?
I tried SELECT DISTINCT country FROM country SET id_country = id_country + 1 but it didn't work and I guess it is because SELECT don't mix up the value (that would make sense).
Any help would be gladly accepted.
You need to set up a separate table called country. It will have 2 columns where each country will have an individual id and a name. You can then set the id_country to the id from the country table with the following:
UPDATE table t, country_table c SET t.id_country = c.id WHERE t.country = c.name
The first thing you want to do is to group your table by country, and always select the maximum id for each country.
SELECT description_country, max(id) id
FROM country
GROUP BY description_country
When you have this you want to update your original table with the id that came out of the previous query where the description matches.
UPDATE country c
SET ID_COUNTRY=X.id
FROM
(SELECT description_country, max(id) id
FROM country
GROUP BY description_country) X
WHERE c.description_country = x.description_country
you can use this query:
update country_table set id_country =
(select max_id from (select max(id) as max_id, country from country_table group by country_table.country ) as temp_table
where temp_table.country = country_table.country)
I am not sure how to do this in mysql, i have searched but I can't seem to find a solution.
,
I have a table like so.
id pid occurrence
1 23 blank
2 23 blank
3 44 blank
Basically, occurrence should have the value of 2, for id 1,2 and a value of 1 for id 3.
Any help would be appreciated. I can easily call count and GROUP BY, and get the number of times each one occurance, but I would like to update column occurrence in the right place for each pid.
To get the correct occurrence value you can do
select pid, count(*) as occurrence
from your_table
group by pid
To update the table do
update your_table t1
join
(
select pid, count(*) as occurrence
from your_table
group by pid
) t2 on t1.pid = t2.pid
set t1.occurrence = t2.occurrence
If you want to set the value in the table, use update with a join:
update table t join
(select pid, count(*) as cnt
from table
group by pid
) tt
on tt.pid = t.pid
set t.occurrence = tt.cnt;
I am updating my table setting a field named "status" based on the condition that the total number of distinct rows should be more than 10 and less than 13. The query is as follows:
update myTable set status='Established'
where id IN(select id, count(*) as c
from myTable
where year>=1996 and year<=2008
group by id
having count(distinct year)>=10 and count(distinct year)<=13)
The problem is, I'm getting error1241 that is "operand should contain 1 column"! Could you please advise how can I solve this? Thanks!
The result of the sub query must return only 1 column :
update myTable set status='Established'
where id IN(select id
from myTable
group by id
having count(distinct year)>=10 and count(distinct year)>=13)
In MySQL, an update with a join often performs better than an update with a subquery in the where clause.
This version might have better performance:
update myTable join
(select id, count(*) as c
from myTable
where year >= 1996 and year <= 2008
group by id
having count(distinct year) >= 10 and count(distinct year) <= 13
) filter
on myTable.id = filter.id
set status = 'Established';
I will also note that you have a table where a column called id is not unique among the rows. Typically, such a column would be a primary key, so the having clause would always fail (there would only be one row).
update myTable
set status='Established'
where id IN(select id from myTable
group by id
having count(distinct year)>=10
and count(distinct year)>=13)
You are using IN operator and then you inner query returns two columns id and count(*) it should return only one column back.