Sql: regarding Update statement - mysql

I am trying to update the SUIDMatches column of table1_orig_namesplits table based on number of matches it find in the master_table using the following update statement.
But I am getting an "Error Code: 1054. Unknown column 'al.source_id' in 'on clause'" error.
UPDATE test.table1_orig_namesplits al
SET al.SUIDMatches = (
SELECT count(distinct v.SUID)
FROM test.master_table v
JOIN test.table1_orig a ON al.source_id = a.id_names
WHERE al.name_first = v.FirstName
AND al.name_last = v.LastName
AND a.date_of_birth_dt = v.BirthDate_dt );
There is a source_id column in the table1_orig_namesplits table is matched with the id_names column of table1_orig.
Can anyone please suggest how to fix this update statement?

The problem appears to be that the sub-query attempts to join test.table1_orig to test.master_table using a column from test.table1_orig_namesplits which is not part of the join.
For further testing / analysis some sample data would be helpful, but the basic idea is that you'll either have to join to al in the subquery, or take a different approach.

Related

MySQL WHERE after SET giving Error Code 1054: unknown column

I have two simple databases, 'rawdosingjune' and 'Dosage_June'.
The following SELECT statement returns the correct data:
SELECT * FROM rawdosingjune WHERE rawdosingjune.corrosion = 'TRUE';
However, if I put the same WHERE condition into an UPDATE statement I get an error?
UPDATE Dosage_June SET Corrosion_Basis = rawdosingjune.DosageBasis
WHERE rawdosingjune.corrosion = 'TRUE';
Error code: 1054. unknown column 'rawdosingjune.corrosion' in 'where clause'
I have tried a number of combinations including creating the tables again?
These are the two tables
rawdosingjune:
Dosage_June:
Judging from your tables, Dosage_June.wellname & rawdosingjune.well is the unique link therefore the INNER JOIN can use it for their ON condition.
Something like this should work:
UPDATE Dosage_June INNER JOIN rawdosingjune
ON Dosage_June.wellname=rawdosingjune.well
SET Dosage_June.Corrosion_Basis = rawdosingjune.DosageBasis
WHERE rawdosingjune.corrosion = 'TRUE';
It should not be surprising that trying to use a WHERE clause that names a table not selected by the query, will not work.
In the case of your SELECT, it's valid, because you name a row that's part of the query.
In the case of your UPDATE, it's a completely unrelated row that the engine has no idea what to do with.
Instead, name a row that's actually part of the table(s) you're operating on.

MySQL insert into column data from another table

I have 2 tables:
original {ID, FirstName, LastName}
and
dummy {ID(NULL), FirstName, LastName}
I have to insert into t2.ID the original.ID but only if the FirstName and LastName from both tables are the same. Now, i've tried:
1.Error Code: 1054. Unknown column 't2.FirstName' in 'where clause'
INSERT INTO dummy (ID)
SELECT ID
FROM original
WHERE dummy.FirstName = original.FirstName
AND dummy.LastName = original.LastName;
2.Error Code: 1054. Unknown column 'original.FirstName' in 'where clause'
UPDATE dummy
SET ID = original.ID
WHERE dummy.FirstName=original.FirstName
AND dummy.LastName= original.LastName;
3.Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
NOTE: I have to find a way without disableing safe mode.
UPDATE dummy
JOIN original
ON original.FirstName = dummy.FirstName
AND original.LastName = dummy.LastName
SET dummy.IDPacient = original.ID
WHERE original.ID <> 0;
Now if someone could help me understand what i did wrong in each of these 3 cases and/or give me a better solution, i would much appreciate the help.
Version 1 is just plain wrong - you'll insert a new row, not update the existing row.
Version 2 is close, you just need a join to "original":
UPDATE dummy
SET ID = original.ID
FROM dummy
INNER JOIN original
ON dummy.FirstName =original.FirstName
AND dummy.LastName = original.LastName;
You need to perform a join on first and last name between the "original" and "dummy" tables and then update the ID
Try this..
Update a
Set a.ID = b.ID
From dummy a
Join original b
On a.firstname = b.firstname
And b.lastname = b.firstname
You were trying to filter based on columns in the "original" table but it wasn't included in a from clause in your query.
This link might also have some more useful info for you if you need it.
SQL update query using joins

Convert SELECT statement to an UPDATE statement in MySQL

I have already read a majority of this similarly asked question but due to the specific nature of most of the answers, none helped, or I was unable to apply the answer.
I need to convert this SELECT statement into an UPDATE statement, the SELECT statement works just fine.
SELECT sf_state_filings_cstm.recurrency_c, sf_state_filings_cstm.date_filed_c,
sf_state_filings_cstm.status_c
FROM sc_state_configuration_cstm
INNER JOIN sc_state_configuration_sf_state_filings_1_c ON sc_state_configuration_cstm.id_c = sc_state_configuration_sf_state_filings_1_c.sc_state_c2445uration_ida
INNER JOIN sf_state_filings_cstm ON sc_state_configuration_sf_state_filings_1_c.sc_state_configuration_sf_state_filings_1sf_state_filings_idb = sf_state_filings_cstm.id_c
WHERE sc_state_configuration_cstm.id_c = '2d9b438e-01e1-ccb2-82e5-5721221114bb'
This is what I have so far after working through the following errors:
When I first wrote the update statement, I got:
MySQL code error 1066: Not unique table/alias: ‘sf_state_filings_cstm
Solution: Why does this SQL code give error 1066 (Not unique table/alias: 'user')?
Then I got this error:
1052: Column ‘recurrency_c' in field list is ambiguous
Solution: 1052: Column 'id' in field list is ambiguous
Now I have this error:
Error : Unknown column 'sc_state_configuration_cstm.id_c' in 'where
clause'
None of the below links have helped so far, or I was doing something wrong
Unknown Column In Where Clause
Unknown Column in Where Clause
MySQL: "Unknown column in where clause" during Update Statement
I have a feeling the answer has to do with using an alias as mentioned in one the links, using HAVING instead of WHERE, but just replacing WHERE with HAVING doesn't seem to work.
Here is my syntax right now:
UPDATE `my_database`.`sf_state_filings_cstm`
LEFT OUTER JOIN sc_state_configuration_sf_state_filings_1_c AS joined_tables ON sc_state_configuration_cstm.id_c = sc_state_configuration_sf_state_filings_1_c.sc_state_c2445uration_ida
LEFT OUTER JOIN sf_state_filings_cstm AS main_table ON sc_state_configuration_sf_state_filings_1_c.sc_state_configuration_sf_state_filings_1sf_state_filings_idb = sf_state_filings_cstm.id_c
SET main_table.recurrency_c = 'Perpetual',
main_table.expiration_date_c = ''
WHERE
sc_state_configuration_cstm.id_c = '2d9b438e-01e1-ccb2-82e5-5721221114bb'
EDIT 1:
Here is how the Tables have to be linked to each other:
I also realized, I need to be doing a LEFT OUTER JOIN instead of an INNER JOIN. I have updated the above syntax. The middle table stores the id's from both tables. That's how the relationship is stored. If more information is needed, let me know.
[SOLUTION]
UPDATE sc_state_configuration_cstm
LEFT OUTER JOIN sc_state_configuration_sf_state_filings_1_c ON sc_state_configuration_cstm.id_c = sc_state_configuration_sf_state_filings_1_c.sc_state_c2445uration_ida
LEFT OUTER JOIN sf_state_filings_cstm ON sc_state_configuration_sf_state_filings_1_c.sc_state_configuration_sf_state_filings_1sf_state_filings_idb = sf_state_filings_cstm.id_c
SET sf_state_filings_cstm.recurrency_c = 'Perpetual',
sf_state_filings_cstm.expiration_date_c = null
WHERE
sc_state_configuration_cstm.id_c = '2d9b438e-01e1-ccb2-82e5-5721221114bb'
Thanks to jyncka and a lot of other posts I read, where i noticed that when converting a SELECT to an UPDATE, they simply wrote UPDATE (table from the 'FROM' statement).
I went back and noticed I had written:
FROM sc_state_configuration_cstm
The error is telling you what's happening: id_c is not a column that exists in the sc_state_configuration_cstm table. If you do a DESCRIBE on sc_state_configuration_cstm it will show you the correct field name and you can drop that in instead of id_c.
Or you might have used the wrong alias in your WHERE statement. Without knowing what your tables look like, it's difficult to say which is the case.
Edit: I believe I see the problem. Here are the tables you are explicitly using in the statement:
sc_state_configuration_sf_state_filings_1_c
sf_state_filings_cstm
But you are using this table in the WHERE clause:
sc_state_configuration_cstm
You need to join sc_state_configuration_cstm so that it can be used. It took me a minute to see it because sc_state_configuration_cstm and sf_state_filings_cstm look similar at first glance.

getting unknown column error even when the column is present in mysql table

select tab1.*(
select a.*
from fw_invi a left join fw_resp b on a.id=b.did,
fw_resp fra left join(
select *
from fw_type
) tab4 on fra.qaild=tab4.qdetailid //this causing error
)tab1 left join jos_users u on tab1.consu=u.id order by tab1.createdon desc
On running the above query in mysql i am getting the following error,which should not be the case as the specified missing column is present in that table.i think i am doing the wrong nested table aliasing.
Unknown column 'tab4.qdetailid' in 'on clause'
1.Why i am getting the error even though the column is present.?
2.Is my above query syntax correct?
Thanks in advance.
* can't be used with aliases. Aliases are used for named columns.

Update Table using values from another table

I am trying to update a row called ctcode in a table called partnumber copying values from the row ctcode in a table called families. My SQL statement however returns and "Unknown Column 'families.parent' in 'where clause'" error.
Here is my SQL Statement
UPDATE `partnumber`
SET `partnumber`.`ctcode`=`families`.`ctcode`
WHERE `partnumber`.`partnumber`=`families`.`parent`;
What is wrong with my statement? Is there any more efficient way of doing this?
You can basically join both tables even in UPDATE statements,
UPDATE `partnumber` a INNER JOIN `families` b
ON a.`partnumber` = b.`parent`
SET a.`ctcode`= b.`ctcode`
Works in MySQL 5.5.24-0ubuntu0.12.04.1
UPDATE `partnumber`, `families`
SET `partnumber`.`ctcode`=`families`.`ctcode`
WHERE `partnumber`.`partnumber`=`families`.`parent`;
Try this query
UPDATE partnumber
SET ctcode = f.ctcode
FROM partnumber p
INNER JOIN families f ON p.partnumber = f.parent