选择具有1个值的列名称

发布于 2025-01-24 02:44:38 字数 248 浏览 3 评论 0原文

我有一个这样的Oracle表:

MyTable
MENU  ROLE1  ROLE2  ROLE3
MENU1 1      0      1
MENU2 0      1      0
MENU3 1      1      1

我需要一个查询,可以为我提供具有特定菜单行的值1的列名。

选择column_name 来自mytable 菜单='菜单1'

查询应产生cool1,proam3

I have an Oracle table set up like this:

MyTable
MENU  ROLE1  ROLE2  ROLE3
MENU1 1      0      1
MENU2 0      1      0
MENU3 1      1      1

I need a query that will give me the columns names with value 1 for a specific menu row.

select column_name
from myTable
where menu = 'MENU1'

The query should produce ROLE1,ROLE3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轮廓§ 2025-01-31 02:44:38

如何通过mytable中的所有列循环的函数(菜单),检查其值,以及 - 如果返回1进入集合。

SQL> create or replace function frole (par_menu in varchar2) return sys.odcivarchar2list
  2  is
  3    l_role mytable.role1%type;
  4    retval sys.odcivarchar2list := sys.odcivarchar2list();
  5  begin
  6    for cur_r in (select column_name, column_id
  7                  from user_tab_columns
  8                  where table_name = 'MYTABLE'
  9                    and column_name <> 'MENU')
 10    loop
 11      execute immediate 'select ' || cur_r.column_name || ' from mytable ' ||
 12                        ' where menu = :a' into l_role using par_menu;
 13      if l_role = 1 then
 14         retval.extend;
 15         retval(retval.count) := cur_r.column_name;
 16      end if;
 17    end loop;
 18    return retval;
 19  end;
 20  /

Function created.

样本数据:

SQL> select * From mytable;

MENU       ROLE1      ROLE2      ROLE3
----- ---------- ---------- ----------
MENU1          1          0          1
MENU2          0          1          0
MENU3          1          1          1

测试:

SQL> select * From frole('MENU1');

COLUMN_VALUE
--------------------------------------------------------------------------------
ROLE1
ROLE3

SQL> select * From frole('MENU2');

COLUMN_VALUE
--------------------------------------------------------------------------------
ROLE2

SQL> select * From frole('MENU3');

COLUMN_VALUE
--------------------------------------------------------------------------------
ROLE1
ROLE2
ROLE3

SQL>

How about a function that loops through all columns in MYTABLE (except MENU), checks their values and - if 1 is returned - adds that column into a collection.

SQL> create or replace function frole (par_menu in varchar2) return sys.odcivarchar2list
  2  is
  3    l_role mytable.role1%type;
  4    retval sys.odcivarchar2list := sys.odcivarchar2list();
  5  begin
  6    for cur_r in (select column_name, column_id
  7                  from user_tab_columns
  8                  where table_name = 'MYTABLE'
  9                    and column_name <> 'MENU')
 10    loop
 11      execute immediate 'select ' || cur_r.column_name || ' from mytable ' ||
 12                        ' where menu = :a' into l_role using par_menu;
 13      if l_role = 1 then
 14         retval.extend;
 15         retval(retval.count) := cur_r.column_name;
 16      end if;
 17    end loop;
 18    return retval;
 19  end;
 20  /

Function created.

Sample data:

SQL> select * From mytable;

MENU       ROLE1      ROLE2      ROLE3
----- ---------- ---------- ----------
MENU1          1          0          1
MENU2          0          1          0
MENU3          1          1          1

Testing:

SQL> select * From frole('MENU1');

COLUMN_VALUE
--------------------------------------------------------------------------------
ROLE1
ROLE3

SQL> select * From frole('MENU2');

COLUMN_VALUE
--------------------------------------------------------------------------------
ROLE2

SQL> select * From frole('MENU3');

COLUMN_VALUE
--------------------------------------------------------------------------------
ROLE1
ROLE2
ROLE3

SQL>
握住你手 2025-01-31 02:44:38

您可以为3个角色创建3个视图。

 创建表mytable(
菜单int,
角色1 int,
角色2 int,
角色3 int,
菜单1 int, 
菜单2 int,
菜单3 int);
 
 插入mytable值(1,2,3,4,5,6,7);
 
 

1行影响

 创建视图菜单为
从mytable中选择菜单,cole1,菜单1;
 
 从菜单中选择 * *;
 
菜单|角色1 |菜单1
---:| ----:| ----::
   1 | 2 | 5

db&lt;&gt;&gt;

You can create 3 views for the 3 roles.

create table MyTable(
Menu int,
Role1 int,
Role2 int,
Role3 int,
Menu1 int, 
Menu2 int,
Menu3 int);
insert into MyTable values(1,2,3,4,5,6,7);

1 rows affected

create view Menu1 as
select Menu, Role1, Menu1 from MyTable;
select * from Menu1;
MENU | ROLE1 | MENU1
---: | ----: | ----:
   1 |     2 |     5

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文