简体   繁体   English

更新案例mysql

[英]update join with case mysql

I'm using Mysql and I'm having a difficult time trying to get the results from a update query. 我正在使用Mysql,但在尝试从更新查询中获取结果时遇到了困难。 I am having 2 tables. 我有2张桌子。 First table loaddata_temp and second table section company_category 第一个表loaddata_temp和第二个表节company_category

first table loaddata_temp 第一个表loaddata_temp

|id_external|company_name               |company_category_id|
-------------------------------------------------------------
|          1|iqballord                  |UD                 |
|          2|A Plus Lawn Care           |PT                 |
|          3|A. L. Price                |PMA                |
|          4|A.J. August Fashion Wear   |BUMN               |
|          5|A+ Electronics             |WARUNG             | 
|          6|A+ Investments             |PT                 | 

second Table Company_category 第二表Company_category

|company_category_id|company_category_description|
--------------------|-----------------------------
|                  3|PT                          |
|                  5|UD                          |
|                  6|PMA                         |
|                  7|BUMN                        |
|                 23|Koperasi                    |

I used this query to get results as 我用这个查询来获得结果

UPDATE loaddata_temp,company_category
       SET loaddata_temp.company_category_id= 
        CASE 
            WHEN loaddata_temp.company_category_id = company_category.company_category_description 
            THEN company_category.company_category_id
            ELSE 'error' END

what i expeted from above query 我从上述查询中得出的结论

|id_external|company_name               |company_category_id|
-------------------------------------------------------------
|          1|iqballord                  |5                  |
|          2|A Plus Lawn Care           |3                  |
|          3|A. L. Price                |6                  |
|          4|A.J. August Fashion Wear   |7                  |
|          5|A+ Electronics             |error              | 
|          6|A+ Investments             |3                  | 

but what i get 但是我得到了什么

|id_external|company_name               |company_category_id|
-------------------------------------------------------------
|          1|iqballord                  |error              |
|          2|A Plus Lawn Care           |error              |
|          3|A. L. Price                |error              |
|          4|A.J. August Fashion Wear   |error              |
|          5|A+ Electronics             |error              | 
|          6|A+ Investments             |3                  |

I would use the following query: 我将使用以下查询:

UPDATE      loaddata_temp lt
LEFT JOIN   company_category cc
        ON  cc.company_category_description = lt.company_category_id
    SET     lt.company_category_id = COALESCE(cc.company_category_id, 'error');

Use LEFT JOIN to find matching rows for update. 使用LEFT JOIN查找匹配的行以进行更新。 If no row was found in company_category table then update to 'error'. 如果在company_category表中未找到任何行,则更新为“错误”。

UPDATE loaddata_temp l LEFT JOIN company_category c ON c.company_category_description = l.company_category_id
       SET loaddata_temp.company_category_id= If(c.company_category_id IS NULL, 'error' ,c.company_category_id)

you can also use IF statement in query if company_category_id found then it will update the same otherwise return 'error'. 您也可以在查询中使用IF语句,如果找到company_category_id,则它将更新相同的语句,否则返回“错误”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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