使用所选行的值设置所有行的值

时间:2016-08-30 13:12:26

标签: sql mariadb

所以我有这样的表:

<TextView
    android:text="Test 2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/TexViewStyle"/>`

我想要的是所有这些内容都具有相同的SELECT cl.*, (SELECT count(1) FROM attachments AS at WHERE at.call_id = cl.id) as num_attachments, (SELECT count(1) FROM notes AS nt WHERE nt.call_id = cl.id) as num_notes, FROM calls AS cl ,例如+----+-------+-----+ | id | name | ... | +----+-------+-----+ | 1 | test1 | ... | | 2 | test2 | ... | | 3 | test3 | ... | | 4 | test4 | ... | +----+-------+-----+ ,这取决于name我不想要解决方案,我手动写下该值。因此,当我输入数字test2时,它会将所有行的名称更改为{{1} },因为id

我试过这个命令:

2

我期待的解决方案是:

test2

PS:我的桌子没有这个名字,结构也完全不同,但我用这个jsut作为例子。

2 个答案:

答案 0 :(得分:3)

Update table SET name =
   (Select name 
    from table 
    where id = 2)

如果这在MySQL中不起作用,那么试试这个

Update t SET name =
   (Select name 
    from table 
    where id = 2)
From table t

正确的OP发现的MariaDB语法:

UPDATE table as t, 
     (SELECT name FROM table 
      WHERE id = 2) as temp
  SET t.name = temp.name

答案 1 :(得分:1)

没有更好,但可能不那么神秘:

SELECT @thename := name  FROM table  WHERE id = 2;
UPDATE table  SET name = @thename;