I have a small problem updating rows of one table with data from another one, please help.
Table l with columns
Make
Model
OEMNumberLatest
OemNumberPrevious
cStockCode
cDescription
The two columns cStockCode,cDescription are blank at the moment and are waiting to be populated, and the second table c with columns StockCode and Description
The question is how to move/copy contents of table c into rows of table l?
Where c.StockCode = l.OEMNumberLatest and or c.StockCode = l.OemNumberPrevious
You probably shouldn't move the contents of one table into the other. It is really better to just get the values when you need them, using a join:
select l.*, c.cStockCode, c.cDescription
from table1 l join
c
on c.StockCode = l.OEMNumberLatest or c.StockCode = l.OemNumberPrevious;
This seems like a strange condition, with the or because you can get multiple matches.
In any case, you can convert this to an update easily:
update table1 l join
c
on c.StockCode = l.OEMNumberLatest or c.StockCode = l.OemNumberPrevious
set l.cStockCode = c.cStockCode,
l.cDescription = c.cDescription;
When both conditions match, then one will arbitrarily be used for the update.
This is a example of a update syntax using another table
UPDATE tableL SET
tableL.cStockCode=tableC.StockCode,tableL.cDescription=tableC.Description
FROM tableL
JOIN tableC ON
(tableC.StockCode = tableL.OEMNumberLatest)
OR
(tableC.StockCode = tableL.OemNumberPrevious);
I assument that the contents in tableC is to be filled in tableL , and the JOIN is based on StockCode with OEMNumberLatest or OemNumberPrevious
Try this (save your data before trying, I am not sure of your database structure) :
UPDATE l
SET l.cStockCode = c.StockCode, l.cDescription = c.Description
FROM l
INNER JOIN c ON (c.StockCode = l.OEMNumberLatest OR c.StockCode = l.OemNumberPrevious)
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 code for update statement with left join in it. This code is to update a_status in table a when users insert new data in another table, table b. In short, I want to simultaneously update table a on column a_status, when table b get a new data.
UPDATE a
LEFT JOIN b
ON a_id = b_id
SET a_status = 'Process'
WHERE
b_id = a_id;
This code works and gave me the result I wanted. But I have to implemented this into another way of writing which use array and MySQL::updateData(). The problem is I am not familiar with using this writing syntax. Can anybody help me on how to write it so that I can use left join or anything that can gives out the result I wanted in the above code. Here is what I did so far:
$arr = array("b"=>array( array('b_id','b_modul','b_tkmsk'),
array($_GET['b_id'],$_GET['b_modul'],$_GET['b_tkmsk']),
));
MySQL::insertData($arr);
$arr = array("a"=>array(array('a_status'),
array('Proccess'),
"where a_id = b_id"));
MySQL::updateData($arr);
Using the selected answer here, I attempted to craft a MySQL query that select columns and set values but am getting a 'Every derived table must have its own alias'. I only have two tables: matrix_swfl_res & RLN which I've defined as e & d respectively. What am I missing?
UPDATE (SELECT e.MLSNumber, d.MLSNumber
FROM matrix_swfl_res e, RLN d
WHERE e.MLSNumber = d.MLSNumber)
SET e.RSLN = d.RSLN
If you consider the answer linked, you have to do a Join when you want to update a table
Here the mysql error is because
(SELECT e.MLSNumber, d.MLSNumber
FROM matrix_swfl_res e, RLN d
WHERE e.MLSNumber = d.MLSNumber)
is considerated as a derivated table, as you write it, it's like you want to update this derivated table.
If I understand what you want :
You want to update the table A with some select you have done previously, here is what you need to do :
UPDATE A
INNER JOIN (SELECT e.MLSNumber, d.RSLN
FROM matrix_swfl_res e
INNER JOIN RLN d ON e.MLSNumber = d.MLSNumber ) as Q
ON A.MLSNumber= Q.MLSNumber)
SET A.RSLN = Q.RSLN
I don't have all the tables details, but I hope with this example it will be clearer
The second part of your linked answer is what you need - something like
UPDATE matrix_swfl_res e
INNER JOIN RLN d ON e.MLSNumber = d.MLSNumber
SET e.RSLN = d.RSLN
I write following query but it is not working it shows Query interrupted
update media m ,cities c
set m.latitude=c.latitude
where m.cities_id=c.id;
anyone can know what is mistake in above query???
There is an alternate syntax that allow for joins in an update but this is the standard SQL way. Make sure the inner query only returns a single value.
update media
set latitude = (
select c.latitude from cities c where c.id = media.cities_id
)
Try this query :
UPDATE media m LEFT JOIN cities c on c.cities_id = m.id SET m.latitude=c.latitude;
Note: ensure Both cities_id , id are of same datatype.
UPDATE media m
INNER JOIN cities c ON m.cities_id=c.id
SET m.latitude=c.latitude;
SELECT
id, mapid, life_type, lifeid, x_pos, y_pos, foothold, min_click_pos, max_click_pos, respawn_time, life.flags, script.script
FROM
map_life life
LEFT JOIN
scripts script
ON
script.objectid = life.lifeid
AND
script.script_type = 'npc'
AND
helper = 0
LEFT JOIN
npc_data n
ON
n.npcid = life.lifeid
AND script.script_type = 'npc'
I'm trying to execute the following scripts. Basically, I'm showing all the rows from the table map_life, and also left joining column script from table scripts and column storage_cost from table npc_data if they lifeid column's value match scripts's objectid and npc_data's npcid.
However, it doesn't work properly. Why's that? I can't see the correct values for storage_cost.
Thanks
Your query is missing the [storage_cost] column in the returned data set:
SELECT
life.[id],
life.[mapid],
life.[life_type],
life.[lifeid],
life.[x_pos],
life.[y_pos],
life.[foothold],
life.[min_click_pos],
life.[max_click_pos],
life.[respawn_time],
life.[flags],
script.[script],
npc.[storage_cost]
FROM
[map_life] AS life
LEFT OUTER JOIN [scripts] AS script
ON ( life.[lifeid] = script.[objectid] )
LEFT OUTER JOIN [npc_data] AS npc
ON ( life.[lifeid] = npc.[npcid] )
WHERE
script.[script_type] = 'npc'
AND [helper] = 0
Also, it wasn't clear what table the [helper] column exists in. If the column is in the [scripts] table, then you could modify the query above by changing [helper] = 0 in the WHERE Clause to instead be script.[helper] = 0.
Added:
Naturally, since you are using LEFT OUTER JOINs, the returned values for script.[script] and npc.[storage_cost] may be NULL.
I hope this helps.