java—如果当前发送的数据与前一个相同,则减少前一个数据计数

pvabu6sv  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(297)

例如,假设我有一个数据列表(oracle myu sql)

State   RPD     Mac_address        total_Mac_Online_Count.     
 26      AA      aa:bb:12:cc:ab:aa  1
 26      BB      aa:bb:12:cc:ab:ab  1
 26      CC      aa:bb:12:cc:ab:ac  1
 26      DD      aa:bb:12:cc:ab:ad  1
 26      EE      aa:bb:12:cc:ab:bb  1

在此,我将这样的数据发送到db

26      AB      aa:bb:12:cc:ab:ac

mac地址与rpd“cc”相同。现在总的mac联机计数应该减少一个rpd“cc”

26      CC      aa:bb:12:cc:ab:ac  0

对于新数据,它应该像

26      AB      aa:bb:12:cc:ab:ac  1

提前谢谢。

58wvjzkj

58wvjzkj1#

像这样的?
测试用例:

SQL> create table mac
  2    (state number,
  3     rpd varchar2(2),
  4     mac_address varchar2(20) constraint uk_mac unique,
  5     total_count number
  6    );

Table created.

SQL> insert into mac
  2    select 26, 'aa', 'aa:bb:12:cc:ab:aa', 1 from dual union
  3    select 26, 'bb', 'aa:bb:12:cc:ab:ab', 1 from dual union
  4    select 26, 'cc', 'aa:bb:12:cc:ab:ac', 1 from dual;

3 rows created.

一个过程:我把整行选成一个局部变量。你可能会得到 NO_DATA_FOUND ,我正在处理它(什么也不做)。如果有匹配,我就减去 TOTAL_COUNT 将同一mac地址行的列值乘以1,然后插入整个新行。
然而,这个模型似乎支持重复的mac地址。这合法吗?在这种情况下你想怎么办?例如,如果您再次输入相同的mac地址? SELECT 会回来的 TOO_MANY_ROWS 应该处理的。一个选项是只更新一行(哪一行?);还是。。。?

SQL> create or replace procedure p_mac
  2    (par_state in number, par_rpd in varchar2, par_mac_address in varchar2)
  3  is
  4    l_row mac%rowtype;
  5  begin
  6    select *
  7      into l_row
  8      from mac
  9      where mac_address = par_mac_address;
 10
 11    update mac set
 12      total_count = total_count - 1
 13      where mac_address = par_mac_address;
 14
 15    insert into mac (state, rpd, mac_address, total_count)
 16    values
 17    (par_state, par_rpd, par_mac_address, 1);
 18  exception
 19    when no_data_found then null;
 20  end;
 21  /

Procedure created.

测试:

SQL> select * from mac;

     STATE RP MAC_ADDRESS          TOTAL_COUNT
---------- -- -------------------- -----------
        26 aa aa:bb:12:cc:ab:aa              1
        26 bb aa:bb:12:cc:ab:ab              1
        26 cc aa:bb:12:cc:ab:ac              1

SQL> exec p_mac(26, 'ab', 'aa:bb:12:cc:ab:ac');

PL/SQL procedure successfully completed.

SQL> select * From mac;

     STATE RP MAC_ADDRESS          TOTAL_COUNT
---------- -- -------------------- -----------
        26 aa aa:bb:12:cc:ab:aa              1
        26 bb aa:bb:12:cc:ab:ab              1
        26 cc aa:bb:12:cc:ab:ac              0
        26 ab aa:bb:12:cc:ab:ac              1

SQL>

相关问题