簡體   English   中英

如何創建 SQL-Query 來更新表中的數據?

[英]How to create SQL-Query to update data in tables?

我正在嘗試更新表中的數據。

我有兩個表,t1 和 t2

T1 第一張桌子

T2 第二張桌子

我想做的是,如果 t1 有id_avito = nullall_usl_name = %usl_name1%all_tel = %tel1% ,並且 t2 有id != nullusl_name = %usl_name1%tel = %tel1%

例如,執行查詢后的 t1 必須是這樣的更新 t1

update people.t1, people.t2
set 
    id_avito = people.t2.id, 
    lnk_avito = people.t2.link, 
    all_price = people.t2.price,
    all_date = people.t2.date, 
    all_adr = people.t2.adr,
    all_usl_name = people.t2.usl_name 
where id_avito != people.t2.id
and all_tel= people.t2.tel 
and all_usl_type = people.t2.usl_type

我嘗試這樣做,但它不起作用

UPD

示例: 表格 更新前、更新后和第二個表

嘗試使用更新連接,您還需要在使用字符串搜索的地方使用 like 運算符

update people.t1 a  
join people.t2 on id_avito != people.t2.id
and all_tel= people.t2.tel 
and all_usl_type = people.t2.usl_type
set 
    id_avito = people.t2.id, 
    lnk_avito = people.t2.link, 
    all_price = people.t2.price,
    all_date = people.t2.date, 
    all_adr = people.t2.adr,
    all_usl_name = people.t2.usl_name 

請試試這個。

對於 SQL

 UPDATE A
 SET 
    A.id_avito = B.id, 
    A.lnk_avito = B.link, 
    A.all_price = B.price,
    A.all_date = B.date, 
    A.all_adr = B.adr,
    A.all_usl_name = B.usl_name 
 FROM 
    people.t1  A
    INNER JOIN people.t2 B
    ON A.id_avito != B.id
    AND A.all_tel= B.tel 
    AND A.all_usl_type =B.usl_type 
where ISNULL(A.id_avito,'') ='' and A.all_usl_name like '%usl_name1%' and A.all_tel like '%tel1%' and B.id is not null and B.usl_name like '%usl_name1%' and B.tel like '%tel1%'

對於 MYSQL

UPDATE people.t1 a  
INNER JOIN people.t2 B
    ON A.id_avito != B.id
    AND A.all_tel= B.tel 
    AND A.all_usl_type =B.usl_type
   SET 
        A.id_avito = B.id, 
        A.lnk_avito = B.link, 
        A.all_price = B.price,
        A.all_date = B.date, 
        A.all_adr = B.adr,
        A.all_usl_name = B.usl_name 
where IFNULL(A.id_avito,'') = ''  and A.all_usl_name like '%usl_name1%' and A.all_tel like '%tel1%' and IFNULL(B.id,0) <> 0 and B.usl_name like '%usl_name1%' and B.tel like '%tel1%'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM