comparing values from two tables to update a third - ms-access

Im trying to update a field in table c based on the results of a comparison between table a and table b. It goes like this:
table a contains the patient name, status and description of their status.
- This is a complete table meant for comparison.
table b contains the patient name and status.
- This table is added to every so often.
table c is the target table which needs to have a particular field updated based on the results from table a and b.
My logic goes like this, so far:
UPDATE tblc
SET patntStatus to results from comparison of table a & table b.
I know I need a JOIN but am unclear as to whether I need one or two - for example join a and b or join the results of a and b to c?
I think the first one is more correct so what is the proper syntax for the update?
Thanks
**UPDATE
Have added the SQL statement that displays what i want to to add to table c
SELECT STATUS, STATUS_DESCRIPTION
FROM tbla INNER JOIN tblb ON
tbla.STATUS = tblb.STATUS
WHERE tblb.STATUS = tbla.STATUS;

You are close to what you need. I don't know what you want to update in C, but assuming that you want Staus_Desc in tblc to match Status_Description in the comparison of A and B:
Save your SQL in a query (e.g. qry_A_B_Compare)
Create an UPDATE query with the following SQL:
:
UPDATE tblc
INNER JOIN qry_A_B_Compare ON tblc.Status = qry_A_B_Compare.Status
SET tblc.Status_Desc = [Status_Description];

Related

In SQL when we join tables, is there a name for the new table?

E.g. in Pandas, we can apply a mask and create a new dataframe and assign it a name. Similarly in SQL, once I do a LEFT JOIN of 2 tables, is there a way to refer to the new combined table ?
You can join two table and can get the result in the new combined and also you can give name to that table . Just try this query and if get any doubt just feel free to ask anytime.
MYSQL QUERY
EMP(C1, C2, CD1)
DEPT(D1, D2)
SELECT NEWTABLE.First, NEWTABLE.Third
FROM
(SELECT E.C1 AS First, E.C2 AS Second, D.D2 AS Third FROM EMP E, DEPT D WHERE
E.CD1 = D.D1) NEWTABLE
WHERE NEWTABLE.Second > 20;
We have created a virtual table i.e "NEWTABLE" you can give your name also .
(SELECT E.C1 AS First, E.C2 AS Second, D.D2 AS Third FROM EMP E, DEPT D WHERE
E.CD1 = D.D1)
This is the query for where we have applied join query and also we have selected the three row from two table and renamed it as "FIRST", "SECOND" and "THIRD".
And you will get the doubt in the first line so let me clear we have performed the operation NEWTABLE.Second > 20;on the new table which we obtained after join.
If you still get any doubt regarding the query just ask .
Values Stored in the new table is temporary and you can use it for that query only.
And if you want to store permanent value then you have create to new table then assigned that table with the table we joined and so on .
No that won't work in sql, at least not directly
But you can do a subquery
Like
SELECT aa.*
FROM
(SELECT t1.*,t2.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.refid) aa
or A view
CREATE VIEW v AS SELECT t1.*,t2.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.refid;
A problem can result, when you have in both tables the same names for columns, that would cause problems, so you must check and in case of equal columnames alias the second column

update blank records based on join with another table

I have a table in which certain records contains one column value as ZERO. now I want to write a job which will take records from another table based on key and update in first table.
its kind of update table A based on join with table B where A certain records are blank.
I tried following syntax with temporary table to first get all records which contains zero for that column and then using join to three tables , one in which need to update , one is temporary table and last one from where i will get the original value.
DROP TABLE IF EXISTS `blank_raw_ids`;
CREATE TEMPORARY TABLE blank_raw_ids (SELECT * FROM ct where ct.rawid=0);
UPDATE ct
INNER JOIN main_raw_ids as m ON ct.bid=m.bid
INNER JOIN blank_raw_ids as r ON r.bid =m.bid
SET ct.rawid=r.rawid;
I also feel that I am not able to properly use the temp table as i am still joining with main table and blank table.
But the blank_raw_ids only has rows where rawid=0, so if you set ct.rawid=r.rawid you will always set 0 = 0 which has no net effect.
I think I understand you want to copy the values of main_raw_ids.rawid to replace ct.rawid where ct.rawid is 0.
Wouldn't the following do that?
UPDATE ct
INNER JOIN main_raw_ids AS m ON ct.bid=m.bid
SET ct.rawid = m.rawid
WHERE ct.rawid = 0;
No need for a temporary table.

Execute value in two table

I want to execute value from two tables. I have write query to execute value but i don't know is it wrong or true. I provide in following in query.
"SELECT a.id,a.name,b.address,b.pin FROM table1 a,table2 b WHERE a.id=b.id";
You want to JOIN the two tables. You are trying with an implicit JOIN notation that is deprecated and you should do it with an explicit JOIN like this:
SELECT a.id,a.name,b.address,b.pin
FROM table1 a JOIN table2 b ON a.id=b.id
This is untested since you didn't provide examples of your data but you can read it as:
Select id and name from table a, address and pin from table b joining
them on the id field of each that must match. Swow only those records
that match.
You can read more here

Merging 2 tables preserving the ID

I have a question about merging a table with another preserving an ID on a database (I'm using MySQL). I have 2 tables, the first has and Item ID and a category and subcategory assigned to that ID. The second has a Item ID with all its characteristics like name and other variables. How can I merge those two tables in a way that the ID corresponds to the correct item in the new table (that's the difficult part I think)? Is it possible?
Thank you for all the help!
It's a very basic operation called Inner Join:
Select *
from table1
inner join table2
on table1.itemid = table2.itemid;
EDIT: As OP wants to create a new table with the fields return by above query and insert data into newly created table; following are the query to insert data once its created:
Insert into tablename
Select *
from table1
natural join table2;
Note: Make sure that the order and datatypes of columns in new table and in the result of above select query must be same.
I'm assuming you want to create table from the combined results. See this page for details.
Basically you write and test the SQL query then CREATE TABLE table_name AS sql_query
create table new_item_table
as
select
a.item_id,
a.category,
a.subcategory,
b.item_name,
b.item_char_1,
b.item_char_2
from
item_category a inner join item_char b on a.item_id = b.item_id;
This will Do:
select a.*,b.ItemName,b.ItemChar1,b.ItemChar2 from FirstTable a join select * from SecondTable b on a.ItemId=B.ItemId;
Use left join if some of the records are not there in the second table

Append records from one table to another using the common field

There are many varied posts about this matter, but I am unable to find the answer I need. I am hoping this question is unique.
I am trying to append all the data from one table to another, without creating new records. The data in the second table is really a subset of data for a portion of the existing records in the first table.
For example:
I have the table "SPK". And I want to write all of the data from SPK into the table "RCT". The common field between each record I want to match is the RegID, which is unique in both tables (i.e. there is only one SPK record per RCT record).
If I understand correctly, you mean append the columns in one table (call it SECOND) to the other (call it FIRST).
In that case, does this work ?
UPDATE
regcontactsTest
JOIN
speakersTest
ON speakersTest.RegistrationID = regcontactsTest.RegistrationID
SET regcontactsTest.presentationtitle = speakersTest.presentationtitle
EDIT: Updated the query based on Mariadb syntax
You need to use JOIN. For general Update join :
update tab1 a
join tab2 b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
Or in other words:
update
tab1 a
join tab2 b on ..
set a.field=...;