I need to run an UPDATE query:
UPDATE products
SET fcategory = SELECT fcategory
FROM categories
WHERE categories.scategory = products.scategory
i.e. for the rows in "products" where column scategory = categories.scategory, products.fcategory must be updated to categories.fcategory
Example:
categories.scategory monkies, categories.category gorilla
products.scategory monkies
=> products.fcategory must be updated to gorilla since products.scategory = monkies = products.scategory
Anyone knows how to write such an UPDATE query?
Thanks.
Use a JOIN
UPDATE products AS p
JOIN categories AS c ON p.scategory = c.scategory
SET p.fcategory = c.scategory
Related
I have the following query:
SELECT games_atp.ID1_G, odds_atp.K1
FROM games_atp LEFT JOIN odds_atp ON (games_atp.ID1_G = odds_atp.ID1_O) AND (games_atp.ID2_G = odds_atp.ID2_O) AND (games_atp.ID_T_G = odds_atp.ID_T_O) AND (games_atp.ID_R_G = odds_atp.ID_R_O)
I know the joining is convoluted but the original db is built without a primary key. The above works fine and importantly pulls all the records from games_atp. I now want to add a criteria into this to pull only certain K1 records from odds_atp. I added a WHERE clause as follows:
SELECT games_atp.ID1_G, odds_atp.K1
FROM games_atp LEFT JOIN odds_atp ON (games_atp.ID1_G = odds_atp.ID1_O) AND (games_atp.ID2_G = odds_atp.ID2_O) AND (games_atp.ID_T_G = odds_atp.ID_T_O) AND (games_atp.ID_R_G = odds_atp.ID_R_O)
WHERE (((odds_atp.ID_B_O)=2));
However, this overides the left join and only pulls records from games_atp where there is a corresponding record in odds_atp with ID_B_O = 2. How do I keep the criteria and all the records in games_atp? Thanks in advance.
Your current where condition will filter your final result, hence you are only seeing id_B_O = 2.
However, you could also add the wehre condition directly into your left join.
something like this.
SELECT
games_atp.ID1_G, odds_atp.K1
FROM
games_atp
LEFT JOIN odds_atp ON
(
(odds_atp.ID_B_O =2)
AND
(
(games_atp.ID1_G = odds_atp.ID1_O)
AND (games_atp.ID2_G = odds_atp.ID2_O)
AND (games_atp.ID_T_G = odds_atp.ID_T_O)
AND (games_atp.ID_R_G = odds_atp.ID_R_O)
)
);
or you could also take advantage of sub-queries
I have a big table (addon_values_storage) where some additional product description is stored.
The column container_id represents the product id. Each product can have multiple entries in this table (addon_values_storage). All rows with the same container_id (which is unique) represent the additional description of one product.
No my problem:
I want to update some description with conditions.
- only update within same container_id
- only update when some information exists within this container_id range
I tried something like this but i think this is not right:
UPDATE addon_values_storage
SET addon_value = "TEXT"
WHERE addon_key = "products_pc_tower_detail"
AND EXISTS (SELECT *
FROM addon_values_storage
WHERE addon_values_storage.container_id = addon_values_storage.container_id
AND addon_values_storage.addon_key = 'products_pc_groupid'
AND addon_values_storage.addon_value = 'CL-AM4-iGPU');
This is how the table looks like:
(picute shows only some rows of container_id = 8, but there are many more 1-1100 container_id's each unique id has about 50 rows...)
Does this achieve what you want?
(it should update addon_values for all entries having "products_pc_tower_detail" as addon_key only if there is another entry in the table with the same container_id and with addon_key = 'products_pc_groupid' and addon_value = 'CL-AM4-iGPU')
UPDATE addon_values_storage avs1
JOIN addon_values_storage avs2
ON avs1.container_id = avs2.container_id
AND avs2.addon_key = 'products_pc_groupid'
AND avs2.addon_value = 'CL-AM4-iGPU'
SET avs1.addon_value = "TEXT"
WHERE avs1.addon_key = "products_pc_tower_detail"
Edit: fixed the typo :)
With a self join and the conditions in the WHERE clause:
update addon_values_storage t1
inner join addons_values_storage t2 on t1.container_id = t2.container_id
set t1.addon_value = "TEXT"
where
t1.addon_key = "products_pc_tower_detail"
and t2.addon_key = 'products_pc_groupid'
and t2.addon_value = 'CL-AM4-iGPU'
I read that using IN in MySql queries causes a slowdown in performance, unlike in Oracle and that a JOIN should be used instead. I am able to convert simple queries, but I am struggling with more complex ones that contain nested SELECTs. For example:
update Records set ActiveStatus=0, TransactionStatus=0, LastUpdated=&
where ActiveStatus!=5 and LastUpdated!=&
and Pk in (select RecordPk from GroupRecordLink
where GroupPk in (select Pk from Groups where ActiveStatus!=5 and
DateTimeStamp>&))
I had a go rewriting it by following this post , but I am not sure my result is correct.
update Records r join (select distinct RecordPk from GroupRecordLink grl join
Groups g on grl.Pk = g.Pk where g.ActiveStatus!=5 and g.DateTimeStamp>&) s
using (Pk) set ActiveStatus=0, TransactionStatus=0, LastUpdated=&
where ActiveStatus!=5 and DateTimeStamp>&
Thanks
Try this:
UPDATE Records
INNER JOIN GroupRecordLink ON Records.Pk = GroupRecordLink.RecordPk
INNER JOIN Groups ON GroupRecordLink.GroupPk = Groups.Pk AND Groups.ActiveStatus != 5
SET Records.ActiveStatus = 0,
Records.TransactionStatus = 0,
Records.LastUpdated = &
WHERE Records.ActiveStatus != 5
AND Records.LastUpdated != &;
In Mysql you could use an explicit join for update (you must change & witha proper value )
update Records
INNER JOIN GroupRecordLink on GroupRecordLink.RecordPk = Records.PK
INNER JOIN Groups on ( GroupRecordLink.GroupPk = Groups.Pk
and Groups.ActiveStatus!=5
and Groups.DateTimeStamp>&)
set ActiveStatus=0,
TransactionStatus=0,
LastUpdated=&
OK I have 2 tables:
holdings: (id, long_name, value, date, sedol)
asset_key: (id, long_name, sedol)
My issue is that in holdings there are many records where the sedol wasn't filled in. I have the asset_key table however that maps a given long_name to a sedol.
Is there a query that can populate holdings.sedol with the result from asset_key?
Something like:
UPDATE holdings SET holdings.sedol =
SELECT asset_key.sedol FROM asset_key
WHERE sedol.long_name = asset_key.long_name
This will do the trick:
UPDATE
holdings
LEFT JOIN asset_key ON sedol.long_name = asset_key.long_name
SET
holdings.sedol=asset_key.sedol
This should work:
UPDATE `holdings`
SET `holdings`.`sedol` = (SELECT `asset_key`.`sedol`
FROM `asset_key`
WHERE `asset_key`.`long_name` = `holdings`.`long_name`)
However, if I am not wrong, you should be sure that this SELECT subquery returns only one row or MySQL will throw an error.
Try the below Query:
update holdings
SET holdings.sedol = asset_key.sedol
from holdings
inner join asset_key on sedol.long_name = asset_key.long_name
Note: The inner join should result in single value only
I have tow tables, the first is mda_alert_info and the second is key_contacts_info. For each alert set up there may be multiple corresponding contacts. Both tables are linked by 3 columns mda_id, stage_id and ref_number and during the query I will have to pass figures values for them
How do I get all what I want from both tables in one statement. Below are the individual SELECT statements.
$result = mysql_query("SELECT `mda_name`, `project_name`, `ipc_id` FROM `mda_alert_info` WHERE `stage_id`=1 AND `mda_id`=2 AND `ref_number`= '444'");
This will always return one record
$result = mysql_query("SELECT `contact_role`, `contact_email`, `contact_ph_number` FROM `key_contacts_info` WHERE `stage_id`=1 AND `mda_id`=2 AND `role`=0 AND `ref_number`='444'");
This may return multiple records
I tried for a while and couldn't get it to work so I tried adding another field called 'me' to both tables which is basically concatting mda_id and stage_id strings then I tried the query below.
$result = mysql_query("SELECT key_contacts_info.contact_role, key_contacts_info.contact_email, key_contacts_info.contact_ph_number, mda_alert_info.mda_name, mda_alert_info.project_name, mda_alert_info.ipc_id FROM key_contacts_info LEFT JOIN mda_alert_info ON key_contacts_info.me = mda_alert_info.me WHERE stage_id=1 AND mda_id=2 AND role=0 AND ref_number='444'");
But it's still not working. What am I doing wrong and how do I get it to work.
UPDATE:
Sorry but I should clarify something on the relationship between the tables the three columns exist on each table but are not primary keys on either of the tables
SELECT a.*, b.*
FROM mda_alert_info a
INNER JOIN key_contacts_info b
ON a.mda_id = b.mda_id AND
a.stage_id = b.stage_id AND
a.ref_number = b.ref_number
WHERE b.role = 0 AND
a.stage_id = 1 AND
a.mda_id = 2 AND
a.ref_number = '444'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
select
a.mda_name,
a.project_name,
a.lpc_id,
k.contact_role,
k.contact_email,
k.contact_ph_number
from mda_alert_info a
join key_contacts_info k
on a.state_id = k.state_id
and a.mda_id = k.mda_id
and a.ref_number = k.ref_number
where a.state_id = 1
and a.mda_id = 2
and a.ref_number = '444'
and k.role = 0
;