I understand basically the concepts of UPDATE to use data in one table to update another similar table. However the table data I have to update to is arranged in a 'vertical' manner as opposed to the 'horizontal' manner of the input table. This query works if I limit it to just one record :
SELECT #userid:=user_id,#club:=CLUB, #financialdate:=FINANCIALDATE
FROM wpty_sa_tmp_update where user_id = 1;
UPDATE wpty_cimy_uef_data SET VALUE = #financialdate WHERE user_id = #userid and field_id = 16;
UPDATE wpty_cimy_uef_data SET VALUE = #club WHERE user_id = #userid and field_id = 8;
If I remove the WHERE user_id clause, it does not update .. what am I missing?
Obviously I can't create a join of any sort because the 2 tables don't share a common ID or key
cheers
You could actually do this from a single update statement:
UPDATE wpty_cimy_uef_data wc
INNER JOIN wpty_sa_tmp_update ws
ON wc.user_id = ws.user_id AND ws.user_id = 1
SET
VALUE = CASE field_id WHEN 16 THEN ws.FINANCIALDATE
WHEN 8 THEN ws.CLUB END
WHERE
field_id IN (8, 16);
Related
How to update table based on another table column?
Here is my SQL. I'm using MySQL.
UPDATE tb_notify SET alert = '0' WHERE post_id = '01' AND (HERE IS I WANT TO GET BASED ON ANOTHER TABLE)
Another table is tb_post with same parameter in each table. That's post_id.
tb_notify
have 3 column:
1. com_id
2. post_id
3. alert
tb_post
have 3 column:
1. post_id
2. uid_post <-- Here I want to based on this column
3. post
Please help.
Thanks
You can use join to do the update
update tb_notify tn
join tb_post tp on tp.post_id = tn.post_id
set
tn.alert = '0'
where
tn.post_id = '01'
AND tp.uid_post = {whatever you want}
I have an issue with a SQL query/SP where I'm trying to update a table that has missing data in specific fields from another table where the data in those same fields exists and is valid. The trick here is I would like to anchor on a value in the first table. I can make it work with a INSERT INTO / SELECT FROM combo, but it creates a duplicate record.
Im using mysql 5.x. Here are the details. The table with missing data is thisweek and the table with valid data is lastweek. Field 1 is MACAddress (which exists and is the anchor) and exists in both tables (for ex. BE:EF:BA:BE:CA:FE), Fields 2-10 in thisweek are blank (''), but there is data in those same fields(Fields2-10) in table lastweek.
UPDATE thisweek
SET thisweek.field2 = lastweek.field2
where thisweek.MACAddress = lastweek.MACAddress and thisweek.filed2 = '';
I know that query isn't anywhere close, so looking for help. Again, the same MACAddress exists in both tables, with the only difference between tables being that field2 in thisweek is blank (and it shouldn't be) and needs to be equal to lastweek.field2 for that MACAddress.
Thanks all.
I think you want the following:
UPDATE tw
SET tw.Field2 = lw.Field2
FROM
ThisWeek tw
JOIN LastWeek lw ON tw.MACAddress = lw.MACAddress
WHERE
tw.Field2 = ''
you need to JOIN thisweek and lastweek tables.
UPDATE thisweek
JOIN lastweek
ON thisweek.MACAddress = lastweek.MACAddress
AND thisweek.field2 =''
SET thisweek.field2 = lastweek.field2
update table1
inner join table2 on table1.id = table2.id and (table1.name = '' or table1.name is null)
set table1.name = table2.name;
I have: something like
UPDATE table
SET field = (SELECT field FROM another_table WHERE id = #id);
Problem: SELECT field FROM another_table WHERE id = #id subquery can return one field or EMPTY SET.
Question: How to handle situation when subquery returns empty set?
Updated:
UPDATE table t
SET field = IF((SELECT field FROM another_table WHERE id = #id) IS NOT NULL, -- select field
(SELECT field FROM another_table WHERE id = #id), -- Problem #1: select field AGAIN!
(SELECT field FROM table WHERE id = t.id) -- Problem #2: try to not change value, so select the current field value!!
);
If function can be useful:
UPDATE table
SET field = if((SELECT field FROM another_table WHERE id = #id) IS NULL,true,false);
You can add the conditional:
WHERE (SELECT COUNT(*) FROM another_table WHERE id = #id) > 0
This will make sure that at least one row exists in another_table with the id. See my SQL Fiddle as an example.
Note: this may not be the most efficient because it does a count on another_table, and if it is greater than 1 it will do another SELECT (two sub-queries). Instead, you can do an INNER JOIN:
UPDATE table
INNER JOIN another_table ON table.id=another_table.id
SET table.field = another_table.field
WHERE another_table.id = #id;
See this SQL Fiddle. The reason why I saved this as a second option, is not all SQL languages can UPDATE with joins (MySQL can). Also, you need some way to relate the tables..in this case I said that the table.id we are updating is equal to another_table.id we are taking the data from.
NOTE The UPDATE statement will modify EVERY row in table and assign the same value to every row; that seems a little unusual.
To answer your question:
If you want to handle the "empty set" by not updating any rows in table, then one way to do this is with a JOIN to an inline view:
UPDATE table t
CROSS
JOIN (SELECT a.field
FROM another_table a
WHERE a.id = #id
LIMIT 1
) s
SET t.field = s.field
Note that if the inline view query (aliased as s) return an "empty set", then no rows in table will be updated, because the JOIN operation will also return an "empty set", meaning there are zero rows to be updated.
What I am trying to do is write some sql code to update a table based of the values in that table dependent of another tables value.
What I have is 2 tables 1)Name 2)CSAL
Name Table has 2 columns that I care about ID and CO_ID the ID is a key field and CO_ID shows is a record is a child of another record.
The CSAL table has the value of MunExpressConsent which can be 1 or 0
what I want to do is if the parent record has the value of 1 in the CASL table I want to update all records that has that ID as CO_ID in the name table to get the value of 1 in the field MunExpressConsent in the CSAL table.
I tried this but is not working:
UPDATE CASL
SET MunExpressConsent = 1
FROM TEST_AMO.dbo.CASL CASL,
TEST_AMO.dbo.CASL CASL_1, TEST_AMO.dbo.Name Name, TEST_AMO.dbo.Name Name_1
WHERE Name.CO_ID = Name_1.ID AND Name_1.ID = CASL.ID AND
Name.ID = CASL_1.ID AND (Name_1.STATUS='a') AND (Name.STATUS='a')
and Name.CO_ID = Name_1.ID
Thank for any assistance
After reading and re-reading your question, I believe that you are looking to do the following:
UPDATE [CASL]
SET [MunExpressConsent] = 1
FROM [CASL]
INNER JOIN [Name]
ON [CASL].[ID] = [Name].[ID]
WHERE [Name].[STATUS] = 'a'
AND [Name].[CO_ID] IN (
SELECT parentName.[ID]
FROM [Name] AS parentName
INNER JOIN [CASL] AS parentCASL
ON parentName.[ID] = parentCASL.[ID]
WHERE parentName.[STATUS] = 'a'
AND parentCASL.[MunExpressConsent] = 1
)
This will update all records in the [CASL] table that have a parent record with [MunExpressConsent] = 1.
NOTE: The parent-child relationship is assumed to be a self-reference within the [Name] table via the following relationship (pseudocode follows): child record [CO_ID] column is equal to parent record [ID] column.
This has me stumped.
MySQL
UPDATE sets SET sets.current_count = (SELECT COUNT(leads_auto.set_id) AS current_count FROM leads_auto WHERE leads_auto.set_id = (SELECT sets.set_id AS setID FROM sets WHERE sets.on_off = 0)) WHERE sets.on_off = 0
Seems right doesn't it? Update the record current_count with the total number of rows in leads_auto which have the set_id value of the set_id of the sets table where the value of its on_off column is 0.
yet I get this error
#1093 - You can't specify target table 'sets' for update in FROM clause
I looked around and someone mentioned that it has to do with the operation being cyclic?
Create a temporary table for the result of SET
UPDATE sets
SET sets.current_count =
(
SELECT COUNT(leads_auto.set_id) AS current_count
FROM leads_auto
WHERE leads_auto.set_id =
(
SELECT set_id
FROM
(
SELECT sets.set_id AS setID
FROM sets
WHERE sets.on_off = 0
) c
)
)
WHERE sets.on_off = 0