mySQL: Where should i put my WHERE? - mysql

I'm trying to add a WHERE clause.
How should i manage it?
This is my Query:
UPDATE usr_time_reg
JOIN users ON usr_time_nr = usr_time_reg.usr_time_nr
SET usr_time_reg.usr_employment = users.employment;
I'm trying to add: WHERE usr_time_reg.usr_emplyoment = "Övrigt"
I've tried:
UPDATE usr_time_reg
JOIN users ON usr_time_nr = usr_time_reg.usr_time_nr,
usr_time_reg.usr_employment = "Övrigt"
SET usr_time_reg.usr_employment = users.employment;
UPDATE usr_time_reg
JOIN users ON usr_time_nr = usr_time_reg.usr_time_nr
SET usr_time_reg.usr_employment = users.employment
WHERE usr_time_reg.usr_employment = "Övrigt";
UPDATE usr_time_reg
JOIN users ON usr_time_nr = usr_time_reg.usr_time_nr
WHERE usr_time_reg.usr_employment = "Övrigt"
SET usr_time_reg.usr_employment = users.employment;
UPDATE usr_time_reg
JOIN users ON usr_time_nr = usr_time_reg.usr_time_nr
AND usr_time_reg.usr_emplyment = "Övrigt"
SET usr_time_reg.usr_employment = users.employment;

Try below as it missing table reference in ON
UPDATE usr_time_reg
JOIN users ON users.usr_time_nr = usr_time_reg.usr_time_nr
SET usr_time_reg.usr_employment = users.employment
WHERE usr_time_reg.usr_employment = "Övrigt";
With JOIN
UPDATE usr_time_reg as tr, users as ur
SET tr.usr_employment = ur.employment
WHERE ur.usr_time_nr = tr.usr_time_nr and tr.usr_employment = "Övrigt";
While joining tables add table alias before field name to avoid ambiguous column error

The following query should work:
UPDATE usr_time_reg u1
INNER JOIN users u2
ON u1.usr_time_nr = u2.usr_time_nr
SET u1.usr_employment = u2.employment;
WHERE u1.usr_emplyment = 'Övrigt';
It can be difficult to keep track of update join syntax for MySQL, in addition to other databases you might be using. So it's always a good idea to have a good reference to use in case you forget.
The main change I made, other than using correct syntax, was to introduce table aliases to the update query. In addition to making it a lot easier to read, it also resolved one of your errors regarding an ambiguous column reference to user_time_nr. Now, it is clear to which table we are referring when we use that column.

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

Delete row if exists

DELETE IF EXIST `#__menu`.*
FROM `#__menu`
LEFT JOIN `#__extensions` ON `#__extensions`.`name` = 'com_view'
WHERE `#__menu`.`component_id` = `#__xtensions`.`extension_id`
AND `#__menu`.`alias` = 'view-sites' AND `#__menu`.`path` = 'view-sites' AND `#__menu`.`title` = 'View sites';
What is wrong in my sql? I think the problem is in IF EXIST, but i could not figure out how to use it on row.
When you're deleting rows from a table, you don't need to use IF EXISTS - you're using a WHERE clause, so if it exists - it will be deleted.
Try changing your query to:
DELETE
FROM `#__menu`
LEFT JOIN `#__extensions` ON `#__extensions`.`name` = 'com_view'
WHERE `#__menu`.`component_id` = `#__xtensions`.`extension_id`
AND `#__menu`.`alias` = 'view-sites' AND `#__menu`.`path` = 'view-sites' AND `#__menu`.`title` = 'View sites';
Also, you don't need to specify ```#__menu.*`` (the columns) to be deleted - you'll just needDELETE FROM...`. Check out here for more info regarding the syntax.

Update Query Issues

Here is my update query.
UPDATE sugarcrm.qb_salesorders_leads_c, sugarcrm.qb_salesorders
SET sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsleads_ida = sugarcrm.qb_salesorders.memo, sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb = sugarcrm.qb_salesorders.id
WHERE sugarcrm.qb_salesorders.id = sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
When I run this it gives me Affected rows 0.
Here is my select statement using the same info that is in the WHERE statement.
SELECT * from qb_salesorders_leads_c, sugarcrm.qb_salesorders
WHERE sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb = sugarcrm.qb_salesorders.id
This returns 354 Rows which is what I am expecting to update on the update query. What am I missing. Please help!
Convert the implicit join to an explicit one:
UPDATE sugarcrm.qb_salesorders_leads_c leads
INNER JOIN sugarcrm.qb_salesorders orders
ON orders.id = leads.qb_salesorders_leadsqb_salesorders_idb
SET leads.qb_salesorders_leadsleads_ida = orders.memo,
leads.qb_salesorders_leadsqb_salesorders_idb = orders.id
As you can see, I also used aliases to make the SQL compact and legible.
To UPDATE using a join, you have to use the explicit join syntax:
UPDATE sugarcrm.qb_salesorders_leads_c
INNER JOIN sugarcrm.qb_salesorders
ON qb_salesorders_leadsqb_salesorders_idb.id
= sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
SET sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsleads_ida
= qb_salesorders_leadsqb_salesorders_idb.memo
, sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
= sugarcrm.qb_salesorders.id