update blank records based on join with another table - mysql

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.

Related

Update query based on multiple criteria only once per row

I am fairly new to ms access (working with access 2013) and unfortunately am stuck with a problem.
I am currently working on an update query with 2 tables. In table 1 I would like to update all fields of a column with a "1" based on multiple criteria. Three different criteria exist in both tables. I only want to update the column if 2 of the criteria are exactly the same in both tables and one criteria is larger in table 2 than in table 1. However, unfortunately even if all criteria match, that does not mean that the certain case is unique. However, I just want to Update a "1" only once per unique row of table 2.
So basically, I have to questions:
Is the current code correct concerning the match I want to make?
Is there any way to tell access to only update once per unique row in table 2?
Thanks a lot for your help!
This is my current code:
UPDATE Table2 LEFT JOIN [Table1] ON (Table2.Criteria1 = [Table1].Criteria1) AND (Table2.[Criteria2] = [Table1].[Criteria2]) SET [Table1].Column = 1
WHERE (((Table2.[Criteria1])=[Table1].[Criteria1]) AND ((Table2.Criteria2)=[Table1].[Criteria2]) AND ((Table2.Criteria3)>=[Table1].[Criteria3]));
A left join and a where on same table work as an inner join. Looking to your code seems you need a join between table1 and table2 for update table2. So the syntax and the condition should be:
UPDATE Table2
SET [Table1].Column = 1
FROM Table2
INNER JOIN [Table1] ON Table2.Criteria1 = [Table1].Criteria1
AND Table2.[Criteria2] = [Table1].[Criteria2]
AND Table2.Criteria3>=[Table1].[Criteria3]
But if you need an update only for a single rows the you could trying using the min(id) resulting from the matching row:
UPDATE Table1
SET [Table1].Column = 1
WHERE Table1.ID = (
SELECT MIN(ID)
FROM Table2
INNER JOIN [Table1] ON Table2.Criteria1 = [Table1].Criteria1
AND Table2.[Criteria2] = [Table1].[Criteria2]
AND Table2.Criteria3>=[Table1].[Criteria3]
)

Select all records in MySQL unless record exist in another table

I have two MySQL tables. One is called match_rail and match_complete.
When a bill_number from match_rail is actioned, the record moves to the match_complete table and should no longer be displayed in the match_rail table.
The match_rail table is refreshed hourly. Therefore I need to make sure not to display the same bill_number if it already exists in the match_complete table.
Here is the query:
SELECT
mr.RAMP
mr.ETA
mr.BILL_NUMBER
// few more columns
FROM
matchback_rail mr
JOIN
matchback_complete mc ON mr.BILL_NUMBER = mc.BILL_NUMBER
The above query gives me 0 records. It should give me all records except the ones that exist in both tables.
Not sure if I should be using a JOIN or LEFT JOIN.
Try this query:
SELECT
mr.RAMP
mr.ETA
mr.BILL_NUMBER
// few more columns
FROM
matchback_rail mr
WHERE NOT EXISTS(SELECT 1 FROM matchback_complete
WHERE BILL_NUMBER = mr.BILL_NUMBER)
You want to use a LEFT JOIN, this gives all records in mr, even if there is nothing joined. Then use WHERE to filter out the ones you don't want.
SELECT
mr.RAMP
mr.ETA
mr.BILL_NUMBER
// few more columns
FROM
matchback_rail mr
LEFT JOIN
matchback_complete mc ON mr.BILL_NUMBER = mc.BILL_NUMBER
WHERE mc.BILL_NUMBER IS NULL

Join more than two tables

I am trying to join 2 tables and in 1 table there may or may not have the corresponding values.but i need to join the table and list the fields as null.
I have tried to join the tables as left join.but If two entries is there in secoond table corresponding to the value of first table,then first table data is displayed twice.
I need to display only one time if there is two data in another table or there is no data in another table,but it should display as null.
Here is my sql query.
SELECT *,incident.incident_id as incidentid
FROM register_incident AS incident
LEFT JOIN incident_images AS im ON im.incident_id=
incident.incident_id
WHERE incident.state='Active'
I need to display only one time each data if there is no corresponding rows in another table,but the fields in the second table list as null.
If there is more than one row in another table,then also display each row in first table one time with one entry in the second table.
you can use select distinct for get only one row eg (limiting the select to indicent_id ,, but you can add the distinctcolumn you need ):
SELECT *,incident.incident_id as incidentid
FROM register_incident AS incident
LEFT JOIN (
select distinct incident_id from
incident_images
WHERE incident.state='Active' ) t ON t.incident_id= incident.incident_id

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=...;

comparing values from two tables to update a third

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];