I have seen very similar if not same questions on here but my trials of trying to convert following query into an UPDATE statement failed.
SELECT table.* FROM table JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column) WHERE t.rank = 1
ORDER BY t.rank DESC
I want to update column of all results selected using the query above.
How can I convert this into an update statement?
Thank you.
This should do it:
update table
set column = 'somevalue'
where id in
(select id from (
SELECT table.* FROM table JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column) WHERE t.rank = 1) x)
not entirely sure but i think it's something like
update tblname set columname = value where tblname.columncompare = (select statement)
INSERT INTO table (id, value)
SELECT table.id, table.value
FROM table
JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column)
WHERE t.rank = 1
ORDER BY t.rank DESC
ON DUPLICATE KEY UPDATE value = VALUES(value)
Insert on duplicate to the rescue!
Basicly this allows you to do any SELECT as normal and then you prepend INSERT INTO and append ON DUPLICATE.
I guess that this query is made up, but what's the point of filtering and ordering the same column?
Related
I want to update a column in a MySQL table that contains string values with values from another table that contains unique names, but without repeating the same values.
Each name in the 'names' table has a unique id from 1 - 1001.
There are 161 names in the target table.
I tried something like this, but it fetches duplicate entries:
UPDATE table_to_update
SET name =
( SELECT name FROM `names`
WHERE id >= (SELECT FLOOR(RAND()*(SELECT MAX(id) FROM `names`)) )
LIMIT 1 ) ;
Here is one option if you are using MySQL 8+, which supports ROW_NUMBER:
UPDATE table_to_update t1
INNER JOIN
(
SELECT id, ROW_NUMBER() OVER (ORDER BY id) rn
FROM table_to_update
) t2
ON t1.id = t2.id
INNER JOIN
(
SELECT name, ROW_NUMBER() OVER (ORDER BY id) rn
FROM (SELECT name, id FROM names ORDER BY RAND() LIMIT 161) t
) t3
ON t2.rn = t3.rn
SET
t1.name = t3.name;
Note that I hard-coded the LIMIT value to be 161 in the subquery, given that you know the size of the target table. This value could be made dynamic as well, but would require more work.
I am using the following query, which I saw from another stackoverflow question but I am getting error.
delete from mytable
where myid not in (
select max(myid)
from mytable
group by myid2)
Error:
#1093 - Table 'mytable' is specified twice, both as a target for 'DELETE' and as a separate source for data
Edit 2:
I also tried this query:
delete from mytable
where myid in (
SELECT
myid, COUNT(*)
FROM
mytable
GROUP BY
myid2
HAVING
COUNT(*) > 1)
And got this error:
#1241 - Operand should contain 1 column(s)
In MySQL, you need to use a JOIN for this purpose. I assume you mean something like this:
delete t
from mytable t left join
(select max(myid) as myid
from mytable
group by myid2
) tt
on t.myid = tt.myid
where tt.myid is null;
Where ? is whatever you really want to group by. Your version will not delete anything because the group by and max() use the same column.
I need to update the column "myColumn" in "myTable" for rows 5 to 10. I thought I can do the following but apparently it's giving me a syntax error:
UPDATE myTable SET myColumn = 'mamal' LIMIT 5,10;
Thanks
SQL tables represent unordered sets. If you have a primary key, you can use that for the ordering.
MySQL does allow limit, but not offset, so you could update the first five rows:
UPDATE myTable
SET myColumn = 'mamal'
ORDER BY id
LIMIT 5;
But not with an offset.
You could get around this with a JOIN:
UPDATE mytable t JOIN
(SELECT id
FROM mytable tt
ORDER BY id
LIMIT 5, 10
) tt
ON tt.id = t.id
SET myColumn = 'mamal';
You can select and update column value based on primarykey of your table.
if you want to change rows limit means you can change limit values.
in below code id Is nothing but Primay Key of your table
UPDATE myTable SET myColumn='mamal' WHERE id IN ( SELECT id FROM ( SELECT id FROM myTable where id ORDER BY id ASC LIMIT 5,5 ) tmp )
TRY by using subquery and join as below and please replace the join column in <column> with real one
UPDATE myTable mt
INNER JOIN (SELECT <column> FROm myTable LIMIT 5,10) t ON t.<column> = mt.<column>
SET mt.myColumn = 'mamal'
Try this:
update myTable
set myColumn = 'mamal'
where your_primary_key
in (select * from (select your_primary_key from myTable limit 5,5) as t);
MySQL does support offset in a limit clause, but only for SELECT. So the correct syntax is: LIMIT offset,row_count. In this case it should be limit 5,5, not limit 5,10. Check: Select Syntax.
You can also use LIMIT in UPDATE, but you can only specify the row_count in this case. Check: Update Syntax.
If you don't have a primary key, just use any unique key.
For why you need a select * from here, it's just a trick to bypass the error discribed here.
I ran into the same problem and saw the answers but after researching I found that you can use "BETWEEN" to UPDATE the Table.
UPDATE myTable
SET myColumn = "mamal"
WHERE id BETWEEN 5 AND 10;
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.
Firstly, if it is relevant, I'm using MySQL, though I assume a solution would work across DB products. My problem is thus:
I have a simple table with a single column. There are no constraints on the column. Within this column there is some simple data, e.g.
a
a
b
c
d
d
I need to get the number/count of values that only appear once. From the example above that would be 2 (since only b and c occur once in the column).
Hopefully it's clear I don't want DISTINCT values, but UNIQUE values. I have actually done this before, by creating an additional table with a UNIQUE constraint on the column and simply INSERTing to the new table from the old one, handling the duplicates accordingly.
I was hoping to find a solution that did not require the temporary table, and could somehow just be accomplished with a nifty SELECT.
Assuming your table is called T and your field is called F:
SELECT COUNT(F)
FROM (
SELECT F
FROM T
GROUP BY F
HAVING COUNT(*) = 1
) AS ONLY_ONCE
select count(*) from
(
select
col1, count(*)
from
Table
group by
Col1
Having
Count(Col1) = 1
)
just nest it a little...
select count( cnt ) from
( select count(mycol) cnt from mytab group by mycol )
where cnt = 1
select field1, count(field1) from my_table group by field1 having count(field1) = 1
select count(*) from (select field1, count(field1) from my_table group by field1 having count(field1) = 1)
first one will return the ones that are unique and second one will return the number of unique elements.
Could it be as simple as this:
Select count(*) From MyTable Group By MyColumn Where Count(MyColumn) = 1
This is what I did and it worked:
SELECT name
FROM people JOIN stars ON stars.person_id = people.id
JOIN movies ON movies.id = stars.movie_id
WHERE year = 2004
GROUP BY name, person_id ORDER BY birth;
note: I was working with several tables here.
CS50 Problem Set 7 (pset7) 9.sql fix!!