使用select中的值更新Mysql

时间:2012-05-10 17:19:03

标签: mysql

我有两个问题。第一个将返回多行:

SELECT parent_entry_id,child_entry_id FROM exp_playa_relationships WHERE parent_field_id = '34';

...我想使用值(parent_entry_id,child_entry_id)并将它们合并到此查询中,替换“x”和“y”,并为第一个查询返回的每一行执行此操作。

UPDATE exp_channel_data AS t1,
(
SELECT field_id_46,field_id_47 FROM exp_channel_data WHERE entry_id = 'x') AS t2
SET t1.field_id_60 = t2.field_id_46, t1.field_id_61 = t2.field_id_47
WHERE t1.entry_id = 'y';

我想我需要使用另一个JOIN,但我无法弄清楚如何在我的例子中实现一个。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:0)

尝试此查询

UPDATE exp_channel_data a1 INNER JOIN exp_playa_relationships a  ON a1.entry_id = a.child_entry_id
INNER JOIN exp_channel_data b ON a.parent_entry_id = b.entri_id 
SET a1.field_id_60 = b.field_id_46, ta1.field_id_61 = b.field_id_47
WHERE parent_field_id = '34'

答案 1 :(得分:0)

我认为这就是你所追求的:

UPDATE exp_playa_relationships AS t0
  JOIN exp_channel_data        AS t1
    ON t1.entry_id = t0.child_entry_id
  JOIN exp_channel_data        AS t2
    ON t2.entry_id = t0.parent_entry_id
   SET t1.field_id_60 = t2.field_id_46
     , t1.field_id_61 = t2.field_id_47

答案 2 :(得分:0)

感谢大家的回复。工作语法是:

UPDATE exp_channel_data AS t1,
(
SELECT 
entry_id as ei2, child_entry_id, parent_entry_id, field_id_46 as f46,field_id_47 as f47 
FROM 
exp_channel_data JOIN exp_playa_relationships ON entry_id=child_entry_id AND parent_field_id = 34) AS t2
SET t1.field_id_60 = f46, t1.field_id_61 = f47
WHERE t1.entry_id=parent_entry_id;

答案 3 :(得分:-1)

或者在更经典的语法中,您需要适应自己的foo& bar属性,但使用如下内容:

update exp_channel_data t1
set (t1.field_id_60,t1.field_id_61) = (
    select t2.field_id_46 , t2.field_id_47
    from exp_channel_data t2
    where 1=1
      and t2.entry_id = 'x'
      and /* ENTER YOUR t1-t2 join condition here */
)
where 1=1
  and t1.entry_id = y
;

但是既然你是MySQL我不相信它支持复合子查询。就这样:

update exp_channel_data t1
set t1.field_id_60 = (
    select t2.field_id_46 
    from exp_channel_data t2
    where 1=1
      and t2.entry_id = 'x'
      and /* ENTER YOUR t1-t2 join condition here */
) , t1.field_id_61 = (
    select t3.field_id_47
    from exp_channel_data t3
    where 1=1
      and t3.entry_id = 'x'
      and /* ENTER YOUR t1-t3 join condition here */
)
where 1=1
  and t1.entry_id = y
;