简体   繁体   中英

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:


  1. 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')?


  1. Then I got this error:

    1052: Column 'recurrency_c' in field list is ambiguous

    Solution: 1052: Column 'id' in field list is ambiguous


  1. 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:

MySQL数据库布局

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM