mysql多表join時(shí)候update更新數(shù)據(jù)的方法
更新時(shí)間:2011年03月31日 00:52:04 作者:
如果item表的name字段為''就用resource_library 表的resource_name字段前面加上字符串Review更新它,他們的關(guān)聯(lián)關(guān)系在表resource_review_link中。
sql語(yǔ)句:
update item i,resource_library r,resource_review_link l set i.name=CONCAT('Review:',r.resource_name) where i.item_id=l.instance_id
and l.level='item' and r.resource_id=l.resource_id and i.name=''
JOIN UPDATE & JOIN DELETE
update a
set a.schoolname = b.schoolname
from tb_Std as a join tb_Sch as b on a.School = b.School
where a.std_year = 2005
go
/*
(2 row(s) affected)
*/
select *
from tb_Std as a join tb_Sch as b on a.School = b.School
/*
A School A A School
2 2005 A A School A A School
3 2004 C A School C C School
4 2005 D D School D D School
(4 row(s) affected)
*/
delete a
from table1 a, table2 b
where a.col1 = b.col1
and a.col2 = b.col2
The above SQL statement runs fine in SQL Server.
If the Oracle 9i has different syntax or if there is any other way to accomplish this with a single delete statement that would be really helpful.
> Hi,
>
> Is the following delete statement possible in Oracle 9i.
>
> delete a
> from table1 a, table2 b
> where a.col1 = b.col1
> and a.col2 = b.col2
>
> The above SQL statement runs fine in SQL Server.
>
> If the Oracle 9i has different syntax or if there is any other way to accomplish this with a single delete statement that would be really helpful.
>
> Thanx in advance.
>
> -Bheem
Bheem,
Try this:
DELETE FROM table1 a where exists (select 1 from table2 b
where a.col1 = b.col1 and a.col2 = b.col2);
Hope this helps,
Tom K.
復(fù)制代碼 代碼如下:
update item i,resource_library r,resource_review_link l set i.name=CONCAT('Review:',r.resource_name) where i.item_id=l.instance_id
and l.level='item' and r.resource_id=l.resource_id and i.name=''
JOIN UPDATE & JOIN DELETE
復(fù)制代碼 代碼如下:
update a
set a.schoolname = b.schoolname
from tb_Std as a join tb_Sch as b on a.School = b.School
where a.std_year = 2005
go
/*
(2 row(s) affected)
*/
select *
from tb_Std as a join tb_Sch as b on a.School = b.School
/*
A School A A School
2 2005 A A School A A School
3 2004 C A School C C School
4 2005 D D School D D School
(4 row(s) affected)
*/
復(fù)制代碼 代碼如下:
delete a
from table1 a, table2 b
where a.col1 = b.col1
and a.col2 = b.col2
The above SQL statement runs fine in SQL Server.
If the Oracle 9i has different syntax or if there is any other way to accomplish this with a single delete statement that would be really helpful.
> Hi,
>
> Is the following delete statement possible in Oracle 9i.
>
> delete a
> from table1 a, table2 b
> where a.col1 = b.col1
> and a.col2 = b.col2
>
> The above SQL statement runs fine in SQL Server.
>
> If the Oracle 9i has different syntax or if there is any other way to accomplish this with a single delete statement that would be really helpful.
>
> Thanx in advance.
>
> -Bheem
Bheem,
Try this:
DELETE FROM table1 a where exists (select 1 from table2 b
where a.col1 = b.col1 and a.col2 = b.col2);
Hope this helps,
Tom K.
相關(guān)文章
MySQL通過(guò)login_path登錄數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例
login_path是MySQL5.6開(kāi)始支持的新特性,本文主要介紹了MySQL通過(guò)login_path登錄數(shù)據(jù)庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02MySQL運(yùn)行報(bào)錯(cuò):“Expression?#1?of?SELECT?list?is?not?in?GR
這篇文章主要給大家介紹了關(guān)于MySQL運(yùn)行報(bào)錯(cuò):“Expression?#1?of?SELECT?list?is?not?in?GROUP?BY?clause?and?contains?nonaggre”的解決方法,文中將解決方法介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06mysql實(shí)現(xiàn)merge into問(wèn)題
文章介紹了在數(shù)據(jù)庫(kù)操作中,如何使用`REPLACE INTO`和`INSERT INTO ON DUPLICATE KEY UPDATE`語(yǔ)句進(jìn)行數(shù)據(jù)更新和插入操作,如果不想創(chuàng)建唯一性索引,可以通過(guò)存儲(chǔ)過(guò)程實(shí)現(xiàn),文章通過(guò)實(shí)驗(yàn)和驗(yàn)證,展示了這兩種方法的實(shí)際效果2024-12-12mysql存儲(chǔ)過(guò)程如何利用臨時(shí)表返回結(jié)果集
這篇文章主要介紹了mysql存儲(chǔ)過(guò)程如何利用臨時(shí)表返回結(jié)果集,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09mysql一條sql查出多個(gè)條件不同的sum或count問(wèn)題
這篇文章主要介紹了mysql一條sql查出多個(gè)條件不同的sum或count問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05MySQL存儲(chǔ)引擎應(yīng)用場(chǎng)景MyISAM?vs?InnoDB優(yōu)勢(shì)選擇
這篇文章主要為大家介紹了MySQL存儲(chǔ)引擎應(yīng)用場(chǎng)景MyISAM?vs?InnoDB優(yōu)勢(shì)選擇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10