使用IN子句和子查询更新MySQL
我想在我的MySQL数据库上运行一些更新。以下是我要使用的查询:使用IN子句和子查询更新MySQL,mysql,sql,Mysql,Sql,我想在我的MySQL数据库上运行一些更新。以下是我要使用的查询: UPDATE `wphh_wp_eStore_tbl` SET `wphh_wp_eStore_tbl`.description = '<div class="dwrp"> <h2>F. Locker $109.99</h2> <h2>Nike Outlet $109.99&
UPDATE `wphh_wp_eStore_tbl`
SET `wphh_wp_eStore_tbl`.description = '<div class="dwrp">
<h2>F. Locker $109.99</h2>
<h2>Nike Outlet $109.99</h2>
<h2>Ch. Sports $107.99</h2>
<h2>Hoopers Hookup $89.99</h2>
<h2 class="special">These prices as of 11/20/13</h2>'
WHERE `wphh_wp_eStore_tbl`.id in (
SELECT `wphh_wp_eStore_tbl`.id FROM `wphh_wp_eStore_tbl`
INNER JOIN `wphh_wp_eStore_cat_prod_rel_tbl`
on `wphh_wp_eStore_cat_prod_rel_tbl`.prod_id = `wphh_wp_eStore_tbl`.id
WHERE `wphh_wp_eStore_cat_prod_rel_tbl`.cat_id = 5
)
这将生成以下错误:
#1093
-不能为FROM子句中的更新指定目标表“wphh\u wp\u eStore\u tbl”
为什么??我知道在MSSQL中我可以做到:
Update tableone
set columnname = 'xxx'
where id in (
select id
from tableone
where category = 10)
它是有效的
我遗漏了什么?简单的解决方案是将
包含在
列表中的一个附加嵌套级别中。MySQL随后将具体化数据:
UPDATE `wphh_wp_eStore_tbl`
SET `wphh_wp_eStore_tbl`.description = '<div class="dwrp">
<h2>F. Locker $109.99</h2>
<h2>Nike Outlet $109.99</h2>
<h2>Ch. Sports $107.99</h2>
<h2>Hoopers Hookup $89.99</h2>
<h2 class="special">These prices as of 11/20/13</h2>'
WHERE `wphh_wp_eStore_tbl`.id in (
select * from (SELECT `wphh_wp_eStore_tbl`.id FROM `wphh_wp_eStore_tbl`
INNER JOIN `wphh_wp_eStore_cat_prod_rel_tbl` on `wphh_wp_eStore_cat_prod_rel_tbl`.prod_id = `wphh_wp_eStore_tbl`.id
WHERE
`wphh_wp_eStore_cat_prod_rel_tbl`.cat_id = 5) t
)
另一种方法是使用
join
与update
连接,我不是MySQL用户,但你可以这样做吗?我还喜欢将表别名与这些难看的名称一起使用
UPDATE `wphh_wp_eStore_tbl` e
inner join `wphh_wp_eStore_cat_prod_rel_tbl` ecpr
on ecpr.prod_id = e.id
and ecpr.cat_id = 5
set e.description = 'Some Text To Update With'
在SQL Server中,“from”和联接位于集合but之后。。mySQL显然不是这样