Update mysql rows with value extracted from counting another table - mysql

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

Related

How to update a table with more than one value

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

Mysql delete select

DELETE FROM `lif_1`.`geo_patch` WHERE `TerID` IN ( SELECT * FROM (SELECT `TerID` FROM `lif_1`.`geo_patch` WHERE `LevelFlags` = '22' ) AS p);
Seems like it's not woeking, what am I doing wrong?
I would like to find all that have LevelFlags=22, so I can find their TerID and delete them.
You can also use a self join
DELETE t.*
FROM geo_patch t
JOIN geo_patch t1 ON(t.TerID = t1.TerID)
WHERE t1.LevelFlags = '22'

MySQL error: duplicate column

I'm having a bit of a problem with the following MySQL query and I can't find the source of it.
MySQL tells me that
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name
'annonce_dispo_id'
SELECT MAX(max_price) AS `max_price`,
COUNT(*) AS `nb_annonces`,
SUM(nb_dispo) AS `nb_dispo`
FROM
(SELECT `annonce`.`id`,
CEIL(MAX(price)*1.16) AS `max_price`,
COUNT(DISTINCT annonce.id) AS `nb_annonces`,
COUNT(annonce_dispoo.annonce_dispo_id) AS `nb_dispo`,
`annonce_dispo1`.*,
`annonce_dispo2`.*
FROM `annonce`
LEFT JOIN `annonce_dispo` AS `annonce_dispoo` ON (annonce_dispoo.annonceId = annonce.id
AND STR_TO_DATE(annonce_dispoo.dispo_date, '%d/%m/%Y') >= CURDATE())
INNER JOIN `annonce_dispo` AS `annonce_dispo1` ON annonce.id = annonce_dispo1.annonceId
INNER JOIN `annonce_dispo` AS `annonce_dispo2` ON annonce.id = annonce_dispo2.annonceId
WHERE ((annonce.city IN
(SELECT `cities`.`id`
FROM `cities`
WHERE (cities.label LIKE 'lyon%'))
OR annonce.zipcode = 'lyon')
OR (annonce.city LIKE '28674'
OR annonce.zipcode = '28674'))
AND (annonce_dispo1.dispo_date = '27/05/2014')
AND (annonce_dispo1.disponibility = 'available')
AND (annonce_dispo2.dispo_date = '31/05/2014')
AND (annonce_dispo2.disponibility = 'available')
AND (annonce.visible = 1)
AND (annonce.completed = 1)
GROUP BY `annonce`.`id` HAVING (nb_dispo >= 1)) AS `t`
I thought gave a different alias for the table in each JOIN I use them in, and can't really put my finger on what else is possible to output such an error.
Don't select annonce_dispo1.* and annonce_dispo2.* in your subquery, duplicated column names are being returned. Instead select the fields you need and alias accordingly.
SELECT MAX(max_price) AS `max_price`,
COUNT(*) AS `nb_annonces`,
SUM(nb_dispo) AS `nb_dispo`
FROM
(SELECT `annonce`.`id`,
CEIL(MAX(price)*1.16) AS `max_price`,
COUNT(DISTINCT annonce.id) AS `nb_annonces`,
COUNT(annonce_dispoo.annonce_dispo_id) AS `nb_dispo`,
`annonce_dispo1`.field, `annonce_dispo1`.otherfield,
`annonce_dispo1`.field as field2, `annonce_dispo1`.otherfield as otherfield2
FROM `annonce`
LEFT JOIN `annonce_dispo` AS `annonce_dispoo` ON (annonce_dispoo.annonceId = annonce.id
AND STR_TO_DATE(annonce_dispoo.dispo_date, '%d/%m/%Y') >= CURDATE())
INNER JOIN `annonce_dispo` AS `annonce_dispo1` ON annonce.id = annonce_dispo1.annonceId
INNER JOIN `annonce_dispo` AS `annonce_dispo2` ON annonce.id = annonce_dispo2.annonceId
WHERE ((annonce.city IN
(SELECT `cities`.`id`
FROM `cities`
WHERE (cities.label LIKE 'lyon%'))
OR annonce.zipcode = 'lyon')
OR (annonce.city LIKE '28674'
OR annonce.zipcode = '28674'))
AND (annonce_dispo1.dispo_date = '27/05/2014')
AND (annonce_dispo1.disponibility = 'available')
AND (annonce_dispo2.dispo_date = '31/05/2014')
AND (annonce_dispo2.disponibility = 'available')
AND (annonce.visible = 1)
AND (annonce.completed = 1)
GROUP BY `annonce`.`id` HAVING (nb_dispo >= 1)) AS `t`
See here for an example that doesn't work:
http://sqlfiddle.com/#!2/9bb13/1
The problem is that you are selecting all columns in the tables annonce_dispo1 and annonce_dispo2.
The fact that you have attributed different table names doesn't mean that there aren't duplicate column names.
I mean, you should use [Table name].[column name]
Example:
(SELECT `annonce`.`id`,
CEIL(MAX(price)*1.16) AS `max_price`,
COUNT(DISTINCT annonce.id) AS `nb_annonces`,
COUNT(annonce_dispoo.annonce_dispo_id) AS `nb_dispo`,
`annonce_dispo1`.annonce_dispo_id AS `column1`,
`annonce_dispo2`.annonce_dispo_id AS `column2`
I hope I've helped

SQl Update from table of random Names

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

Update table on itself MySQL

I can't figure out how to make this query run.... I want to update the table so that dependent on the uuid it grabs the lowest of the 'last_updated' and updates the 'created_on' ... I keep getting 'cant specify target table' although I don't know why =/ Is it a recursion issue?
UPDATE dlp.address AS t1
SET created_on = (SELECT MIN(last_updated)
FROM dlp.address AS t2
WHERE t1.addressuuid = t2.addressuuid);
UPDATE dlp.address AS t1
INNER JOIN
(
SELECT addressuuid, MIN(last_updated) minDate
FROM dlp.address
GROUP BY addressuuid
) AS t2
ON t1.addressuuid = t2.addressuuid
SET t1.created_on = t2.minDate
You can use :
UPDATE dlp.address AS t1
, (SELECT addressuuid, MIN(last_updated) AS minDate
FROM dlp.address GROUP BY addressuuid) AS t2
SET t1.created_on = t2.minDate
WHERE t1.addressuuid = t2.addressuuid