I have a table refs with citations (AU NAMES, which are separated by ; in one field authors)
and a table called aunames with the AU-NAMES at each row. Now I would like to
update my table aunames with the total number of authors for each citation.
rec ID NAME AUCOUNT
1 3 AU1
2 3 AU2
...
...
How can I do that? (auname_copy is a copy of auname.)
I tried:
update aunames
set aucount = (select count(rec)
from refs
join aunames_copy on refs.id=aunames_copy.id
GROUP BY refs.id) ;
But I get the error:
[Err] 1242 - Subquery returns more than 1 row
WHen I try only one row it works.
update aunames
set aucount = (select count(rec)
from refs
join aunames_copy on refs.id=aunames_copy.id
where refs.id='1'
GROUP BY refs.id )
where id='1';
How can I loop through all rows?
Thanks
You can use the UPDATE ... JOIN ... SET syntax:
UPDATE aunames a
INNER JOIN (
SELECT r.id, count(rec) cnt
FROM refs r
INNER JOIN aunames_copy c ON r.id = c.id
GROUP BY r.id
) x ON x.id = a.id
SET a.aucount = x.cnt
NB: if, as commented by #MadhurBhaiya, you created table aunames_copy as a copy of original table aunames, your query can probably be simplified as:
UPDATE aunames a
INNER JOIN (SELECT id, count(rec) cnt FROM refs GROUP BY id) x ON x.id = a.id
SET a.aucount = x.cnt
Related
I have bd hf3 and 5 tables there:
active_preset with columns (id , preset_id)
preset with columns (id , birja_id, trend_id, fractal, interval_up)
birja with columns (id , name)
trend with columns (id , name)
uq_active_preset with columns (id , birja, trend, fractal, interval_up)
In table preset I have a few records. Some of them are in table active_preset by foreign key preset_id. In table active_preset a few records exist once , a few more than once.
I need to update table uq_active_preset with records from table active_preset disregarding repetitions of records if they are present.
I did query from active_preset and it works good:
SELECT
b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
FROM hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
But I don't know how to update uq_active_preset
I tried this and it returns syntax error:1064 :
UPDATE hf3.uq_active_preset uap SET
uap.birja = st.birja ,
uap.fractal = st.fractal,
uap.trend = st.trend,
uap.interval_up = st.interval_up,
FROM (SELECT b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
from hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
) st
when you make an update using from is like you join the updated table with your query result. So, you need also a where statement in order to tell where those two are connected. Also, don't use alias of your updated table on set statement.
You need something like that:
UPDATE hf3.uq_active_preset uap SET birja=st.birja,fractal=st.fractal,trend=st.trend,interval_up=st.interval_up
FROM (SELECT b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
from hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
) st
where uap.fkey=st.fkey
I have tow tables, first table related to forms and second table relate to adviser.each adviser can add comment for a form.
I tried select some column of two tables. It was works i have last subject of second table (adviser table ) in my result .but i need first subject of second table(adviser table ) .
#DQL
SELECT
f.name,
f.title,
f.conditionResultFinal,
f.conditionResult,
f.formCode,
f.dateInsert,
f.id,
(a.idFormRequestProject),
a.subject as subjectAdvisor
FROM AdminBundle:FormRequestProject f
JOIN AdminBundle:Advisor a
WHERE a.idFormRequestProject = f.id
AND (f.conditionResultFinal = 0 OR f.conditionResult = 0)
AND f.displayStatus = 1
GROUP BY f.id
ORDER by a.id,f.id DESC
Finally i solve this problem .i deleted GROUP By .and add new where ...
SELECT f.name,f.title,f.conditionResultFinal,
f.conditionResult ,
f.formCode,f.dateInsert ,
f.id,(a.idFormRequestProject),
a.subject as subjectAdvisor
FROM AdminBundle:FormRequestProject f
Left JOIN AdminBundle:Advisor a WHERE f.id = a.idFormRequestProject AND a.id =
(SELECT Max (aa.id) FROM AdminBundle:Advisor aa WHERE a.idFormRequestProject =
aa.idFormRequestProject ORDER by aa.id ASC ) AND
(f.conditionResultFinal = 0 OR f.conditionResult = 0 ) AND f.displayStatus =1
ORDER by f.id DESC
Got 2 tables as shown above. I want to update 'leaverecord.Consumed' from 'approved.Consumed' WHERE the leaverecord.name = approved.name and leaverecord.leavetype = approved.leavetype. Tried below query by getting error of 'Invalid use of group function
UPDATE leaverecord r INNER JOIN approved a
ON r.name = a.name
SET r.Consumed = SUM(DATEDIFF(a.todate,a.fromdate))
WHERE r.leavetype = a.leavetype AND
r.name = a.name
Calculate the consumed in a subquery and INNER JOIN it with leaverecord and then do the UPDATE like below. No WHERE clause needed.
Try this:
update leaverecord r
inner join (
select name, leavetype, sum(datediff(todate,fromdate)) consumed
from approved
group by name, leavetype
) a on r.name = a.name
and r.leavetype = a.leavetype
set r.consumed = a.consumed;
I Have a table of random names, simply 3 columns(id, firstname, lastname).
I am trying to have SQL update an entire table of names with a random selected name from this table.
Here is the SQL i am using. It seems to work for some of the records, but it doesn't always do all of them, sometime leaving NULLS, or not always updating all rows. Sometime it runs affecting 9 rows, other times it says affected 11 rows... thoughts?
update TestNames,rndnames
set TestNames.fname = rndnames.FirstName,TestNames.lname=rndnames.LastName
where rndnames.ID=floor(1+(rand()*600))
answer:
update
TestNames left join
(select ID,
floor(1+(rand()*600)) as rndid
from TestNames) as TN on TN.ID=TestNames.id
left join rndnames on TN.rndid=rndnames.id
set TestNames.fname=rndnames.FirstName,TestNames.lname=rndnames.LastName
Here is the query:
update TestNames t cross join
rndnames r
set t.fname = r.FirstName,
t.lname = r.LastName
where r.ID = floor(1+(rand()*600));
It only updates a row in testnames when the random id chosen by the expression matches an id in the table. Are the id values in rndnames all populated?
If your table is not very big and it has an id, here is another approach:
update TestName t join
(select t.*,
(select id from rndnames order by rand() limit 1) as rndid
from testname t
) tr
on t.id = tr.id join
rndnames r
on t.rndid = r.id
set t.fname = r.FirstName,
t.lname = r.LastName;
EDIT:
I think this will also work:
update TestNames t cross join
rndnames r
set t.fname = r.FirstName,
t.lname = r.LastName
where r.ID = (select id
from rndnames
order by rand()
limit 1
);
update
TestNames left join
(select ID,
floor(1+(rand()*600)) as rndid
from TestNames) as TN on TN.ID=TestNames.id
left join rndnames on TN.rndid=rndnames.id
set TestNames.fname=rndnames.FirstName,TestNames.lname=rndnames.LastName
I have the following query:
SELECT int_intrebari.id, COUNT( id_raspuns ) AS nr_raspunsuri
FROM int_intrebari, int_raspunsuri
WHERE int_intrebari.id = int_raspunsuri.id
GROUP BY id
Is it possible to update first table with nr_raspunsuri from the query, without writing a foreach statement?
You can UPDATE with JOIN like so:
UPDATE int_intrebari i1
INNER JOIN
(
SELECT id, COUNT( id_raspuns ) AS nr_raspunsuri
FROM int_intrebari
GROUP BY id
) i2 ON i1.id = i2.id
SET i1.nr_raspunsuri = i2.nr_raspunsuri
You can do it like -
update int_intrebari left join int_raspunsuri on int_intrebari.id =int_raspunsuri.id
set int_intrebari.column_to_update = int_raspunsuri.column_from_update_second_table
UPDATE
(SELECT int_intrebari.id, COUNT( id_raspuns) AS nr_raspunsuri
FROM int_intrebari, int_raspunsuri
WHERE int_intrebari.id = int_raspunsuri.id
GROUP BY id) t1,
int_raspunsuri t2
SET
t2.nr_raspunsuri=t1.nr_raspunsuri
WHERE
t1.id=t2.id