MySQL 5.6 - 复杂的表更新

时间:2013-10-30 01:12:13

标签: mysql

我有两张表如下:

机箱

+-------+--------------+-----------+-----------+
| name  | serialnumber | VCenabled | BCenabled |
+-------+--------------+-----------+-----------+
| ENC01 | 12345        |           |           |
| ENC02 | 45678        |           |           |
| ENC03 | 11222        |           |           |
+-------+--------------+-----------+-----------+

互连

+-------------+-----------+-----------------------+
| description | baynumber | enclosureserialnumber |
+-------------+-----------+-----------------------+
| VC          | 1         | 12345                 |
| VC          | 2         | 12345                 |
| BC          | 3         | 12345                 |
| VC          | 1         | 45678                 |
| BC          | 3         | 45678                 |
+-------------+-----------+-----------------------+

我需要更新VCenabled表格中的BCenabledEnclosure列。 VCenabled应包含Interconnect表中相应行的计数。同样适用于BCenabled

以下是我最终需要做的事情:

+-------+--------------+-----------+-----------+
| name  | serialnumber | VCenabled | BCenabled |
+-------+--------------+-----------+-----------+
| ENC01 | 12345        | 2         | 1         |
| ENC02 | 45678        | 1         | 1         |
| ENC03 | 11222        |           |           |
+-------+--------------+-----------+-----------+

我能够提出这个SQL查询,但是我没有太多运气将其转化为更新。此外,如果我在Flyspeed Query中运行此查询,但如果我在MySQL Workbench中运行它,则会出现1064错误:SQL语法错误。

Select
  enclosure.name,
  enclosure.vcenabled,
  count(*)
From
  enclosure Inner Join
  interconnect On interconnect.enclosureserialnumber = enclosure.serialnumber
Where
  interconnect.description like '%VC%'
Group By
  enclosure.serialnumber

任何帮助都将不胜感激。

4 个答案:

答案 0 :(得分:0)

试试这个:

update enclosure t1, 
(select count(*) as x,enclosureserialnumber from interconnect where description='VC') t2,
(select count(*) as y,enclosureserialnumber from interconnect where description='BC') t3
set t1.VCenabled = t2.x,t1.BCenabled=t3.x
where t1.serialnumber=t2.enclosureserialnumber
and t2.serialnumber=t3.enclosureserialnumber

答案 1 :(得分:0)

Sample

UPDATE 
  Enclosure e
  INNER JOIN (
    SELECT
      enclosureserialnumber,
      SUM(CASE WHEN description = 'VC' THEN 1 ELSE 0 END) AS VC,
      SUM(CASE WHEN description = 'BC' THEN 1 ELSE 0 END) AS BC
    FROM
      Interconnect 
    GROUP BY
      enclosureserialnumber
  ) q1
    ON e.serialnumber = q1.enclosureserialnumber
SET
  VCenabled = q1.VC,
  BCenabled = q1.BC;

答案 2 :(得分:0)

这应该使用带有更新的子查询:

update Enclosure e
join (
  select enclosureserialnumber,
    sum(case when i.description = 'VC' then 1 else 0 end) vcsum,
    sum(case when i.description = 'BC' then 1 else 0 end) bcsum
  from Interconnect i
  group by i.enclosureserialnumber
  ) i
  on e.serialnumber = i.enclosureserialnumber
set e.VCenabled = i.vcsum,
   e.BCenabled = i.bcsum;

答案 3 :(得分:0)

update Enclosure as E1 set E1.VCenabled = (select count(*) from Interconnect as I where I.enclosureserialnumber = E1.serialnumber and I.description = 'VC');

类似于BC

已测试...如果发生任何错误,请检查是否存在任何拼写错误。

输出到我的系统......

http://pastebin.com/B0bUDYX6