updating a column with avg data from another table column - mysql

i wrote a command like this to update a column in one table with avg of columns from another table.. its giving errors
UPDATE college_rating,products set
property1_avg = avg(college_rating.rating1),
property2_avg = avg(college_rating.rating2),
property3_avg = avg(college_rating.rating3),
property4_avg = avg(college_rating.rating4),
property5_avg = avg(college_rating.rating5),
property6_avg = avg(college_rating.rating6),
property7_avg = avg(college_rating.rating7),
property8_avg = avg(college_rating.rating8),
property9_avg = avg(college_rating.rating9),
property10_avg = avg(college_rating.rating10),
property11_avg = avg(college_rating.rating11),
property12_avg = avg(college_rating.rating12),
property13_avg = avg(college_rating.rating13),
property14_avg = avg(college_rating.rating14),
property15_avg = avg(college_rating.rating15)
where products.alias = concat(college_rating.property1,'-',college_rating.property2,'-',college_rating.property3)
group by college_rating.property1,college_rating.property2, college_rating.property3

The MySQL multi-table update syntax does not allow the use of group by.
You can accomplish what you are trying to do by moving the aggregation into a sub-query and joining to that sub-query in the multi-table-update instead.
Something like this should work:
update products p
inner join (
select concat(property1,'-',property2,'-',property3) as alias,
avg(rating1) as property1_avg,
avg(rating2) as property2_avg,
avg(rating3) as property3_avg,
avg(rating4) as property4_avg,
avg(rating5) as property5_avg,
avg(rating6) as property6_avg,
avg(rating7) as property7_avg,
avg(rating8) as property8_avg,
avg(rating9) as property9_avg,
avg(rating10) as property10_avg,
avg(rating11) as property11_avg,
avg(rating12) as property12_avg,
avg(rating13) as property13_avg,
avg(rating14) as property14_avg,
avg(rating15) as property15_avg
from college_rating
group by property1,property2, property3
) as r on r.alias = p.alias
set p.property1_avg = r.property1_avg,
p.property2_avg = r.property2_avg,
p.property3_avg = r.property3_avg,
p.property4_avg = r.property4_avg,
p.property5_avg = r.property5_avg,
p.property6_avg = r.property6_avg,
p.property7_avg = r.property7_avg,
p.property8_avg = r.property8_avg,
p.property9_avg = r.property9_avg,
p.property10_avg = r.property10_avg,
p.property11_avg = r.property11_avg,
p.property12_avg = r.property12_avg,
p.property13_avg = r.property13_avg,
p.property14_avg = r.property14_avg,
p.property15_avg = r.property15_avg;

What is the error that you get? And you need to have a WHERE clause unless you want the UPDATE query to apply to ALL the records

I think you'd need to use sub queries, and I'm not sure if you can update two tables like that in MySQL, at least not without prefixing the attributes.

Related

Update table based on value inside this table

I'm trying to update a tablecolumn based on another value inside this table but I keep getting an error.
My query is as follows:
UPDATE partcreditor
SET partcreditor.creditorid = creditor.creditorid
FROM partcreditor
INNER JOIN creditor ON partcreditor.creditornr = creditor.creditornr
WHERE creditor.relgroupid = 1
AND creditor.creditortypeid = 1
UPDATE partcreditor AS PC
INNER JOIN creditor AS CR ON PC.creditornr = CR.creditornr
SET PC.creditorid = CR.creditorid
WHERE CR.relgroupid = 1 AND CR.creditortypeid = 1
No need to use the FROM clause in the update. As well as use alias name for better readability.

Can't update two tables in one Query

I've got two Queries to Update two tables:
First Table
UPDATE user_info SET `location` = ".$locationid.", `looking_for` = ".$lookingfor." WHERE `user_info`.`user_id` = ".$infoid.";
Second Table
UPDATE user_personality SET `personality` = '".$changedescription."' WHERE `user_personality`.`user_info_id` = ".$infoid.";
And I'm trying to merge those two Queries, using the same statement.
UPDATE user_info, user_personality
SET user_info.location = ".$locationid.", user_info.`looking_for` = ".$lookingfor.", user_personality.personality = '".$changedescription."'
WHERE `user_info`.`user_id` = ".$infoid."
AND `user_personality`.`user_info_id` = ".$infoid."
I'm not receiving any error message, but is not updating.
What am I doing wrong?
Thanks.
Just a guess...
"
UPDATE user_info i
JOIN user_personality p
ON p.user_info_id = i.user_id
SET i.location = $locationid
, i.looking_for = '$lookingfor'
, p.personality = '$changedescription'
WHERE i.user_id = $infoid;
";
If you set the 2 table fields equal to each other in the where clause it should work, so I believe you'd change your where clause to:
WHERE `user_info`.`user_id` = `user_personality`.`user_info_id`
AND `user_info`.`user_id` = ".$infoid."
MySQL definitely supports updating multiple tables, so the where clause that works for a multi table select statement should also work for an update.

sql update with join

I am trying to update some rows in a table from another row in a different table.
this is the sql I have so far:
UPDATE nymb_posts
JOIN nymb_postmeta
ON nymb_postmeta.post_id = nymb_posts.ID
WHERE nymb_postmeta.meta_key = "_wp_attached_file"
AND nymb_posts.post_type = "attachment"
AND nymb_posts.post_parent = "0"
SET nymb_posts.Guid = nymb_postmeta.meta_value
I just get an "error in your SQL syntax".
If I remove the WHERE clause there is no error. If I make it a SELECT insead of an UPDATE the WHERE clause works.
What is wrong with the WHERE clause?
The set goes before the where:
UPDATE nymb_posts JOIN
nymb_postmeta
ON nymb_postmeta.post_id = nymb_posts.ID
SET nymb_posts.Guid = nymb_postmeta.meta_value
WHERE nymb_postmeta.meta_key = "_wp_attached_file" AND
nymb_posts.post_type = "attachment" AND
nymb_posts.post_parent = "0";
Use set before where clause then it will work

HOW do i do a data append against multiple columns

I added a column into table A and right now it is empty. What i want to do is take the phone column from table consumer and input it into appphone of table diabetic as long as the first name, last name, address, city, state, and zip, match up in both tables. Below is the query i have been trying and in theory should work but is not. I keep getting the same error no matter which way i change the query.--
error 'subquery returns more than one row'
UPDATE Diabetic_DB
SET Diabetic_DB.AppPhone = (SELECT Consumer.PHONE FROM Consumer
WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)
WHERE EXISTS (SELECT DISTINCT(PHONE) FROM Consumer WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)
the original query i ran looked like this.
UPDATE Diabetic_DB
SET Diabetic_DB.AppPhone = Consumer.PHONE
WHERE EXISTS (SELECT * FROM Consumer WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)
Try something like this:
UPDATE Diabetic_DB
INNER JOIN Consumer ON
Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip
SET Diabetic_DB.AppPhone = Consumer.PHONE

MySQL LIKE Statement not working

I'm trying to copy data from one table to another.
Here is my statement:
UPDATE tblMerchants T,
retailers R
SET T.linklabel = R.linklabel,
T.logo_image = R.logo_image,
T.screen_image = R.screen_image,
T.category = R.category,
T.meta_description = R.meta_description,
T.meta_title = R.meta_title,
T.meta_keywords = R.meta_keywords,
T.intro = R.intro,
T.permalink = R.permalink,
T.excerpt = R.excerpt,
T.main_link = R.main_link,
T.related_blog_post = R.related_blog_post,
T.active = R.active,
T.homepage_featured = R.homepage_featured
WHERE T.homepageurl LIKE '%R.linklabel%'
For example, T.homepageurl would look like http://www.amazon.com/ and R.linklabel would look like amazon.com. So I can't figure out why its not working. I'm not getting any errors, its just saying 0 rows affected.
You should be able to use CONCAT to do this:
WHERE T.homepageurl LIKE CONCAT('%', R.linklabel, '%');
The concat function is used to concatenate multiple strings together. The reason why it's not working is because it's trying to match "http://www.amazon.com" with "%R.linklabel%" instead of "amazon.com".