0

我有两个表如下:

外壳

+-------+--------------+-----------+-----------+
| 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                 |
+-------------+-----------+-----------------------+

我需要更新表中的VCenabledBCenabledEnclosureVCenabled应该包含表中相应行的计数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

4 回答 4

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

于 2013-10-30T02:08:24.453 回答
0

样本

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;
于 2013-10-30T01:37:55.110 回答
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
于 2013-10-30T01:30:42.157 回答
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;
于 2013-10-30T01:39:03.350 回答