根据Oracle数据库中不同表中的列列表选择列?

时间:2018-05-08 07:22:49

标签: sql oracle select

我在class QaDetailSerializer(ModelSerializer): """Serializer to map the Model instance into JSON format.""" album_name = CharField(source='album_name') artist = StringRelatedField() tracks = TracksSerializer() class Meta: """Meta class to map serializer's fields with the model fields.""" order_by = (('id',)) model = Qa fields = ( 'id', 'album_name', 'artist', 'tracks', ) class TracksSerializer(ModelSerializer): def to_representation(self, value): representation = super().to_representation(value) attributes_dict = representation['tracks'] attribute_keys_sorted = sorted(attributes_dict.keys()) sorted_attribute_dict = collections.OrderedDict() for key in attribute_keys_sorted: sorted_attribute_dict[key] = attributes_dict[key] representation['paraphrases'] = sorted_attribute_dict return representation 的{​​{1}}架构中有一个表T_FUNCTION

HR

我需要一个查询,从Oracle表中选择FUN_ID FUN_CMD ------ --------------------------------- 1 substr(FIRST_NAME,2,2) 2 FIRST_NAME || ' ' || LAST_NAME 列数据。

对于FUN_CMD

示例1:,我需要获得以下SQL的结果:

EMPLOYEES
对于FUN_ID = 1

示例2:,我需要获得以下SQL的结果:

select substr(FIRST_NAME,2,2) from EMPLOYEES;

2 个答案:

答案 0 :(得分:1)

闻起来像是一个动态SQL ,或者可能是一个更好的选择,一个返回refcursor的过程。这是一个例子:

SQL> SELECT * FROM t_function;

    FUN_ID FUN_CMD
---------- ----------------------------------------
         1 substr(ename, 2, 2)
         2 ename ||' '|| job

SQL>
SQL> CREATE OR REPLACE PROCEDURE p_fun (
  2     par_fun_id   IN     t_function.fun_id%TYPE,
  3     p_out           OUT SYS_REFCURSOR)
  4  IS
  5     l_cmd   t_function.fun_cmd%TYPE;
  6     l_str   VARCHAR2 (200);
  7  BEGIN
  8     SELECT fun_cmd
  9       INTO l_cmd
 10       FROM t_function
 11      WHERE fun_id = par_fun_id;
 12
 13     l_str := 'select ' || l_cmd || ' from emp';
 14
 15     OPEN p_out FOR l_str;
 16  END;
 17  /

Procedure created.

测试:

SQL> var l_out refcursor
SQL> exec p_fun(1, :l_out);

PL/SQL procedure successfully completed.

SQL> print l_out

SU
--
MI
LL
AR
ON
AR
LA
LA
CO
IN
UR
DA
AM
OR
IL

14 rows selected.

还有一个:

SQL> exec p_fun(2, :l_out);

PL/SQL procedure successfully completed.

SQL> print l_out

ENAME||''||JOB
--------------------
SMITH CLERK
ALLEN SALESMAN
WARD SALESMAN
JONES MANAGER
MARTIN SALESMAN
BLAKE MANAGER
CLARK MANAGER
SCOTT ANALYST
KING PRESIDENT
TURNER SALESMAN
ADAMS CLERK
JAMES CLERK
FORD ANALYST
MILLER CLERK

14 rows selected.

SQL>

答案 1 :(得分:0)

CASE表达式对您的案例有用:

select
  (CASE FUN_ID WHEN 1 THEN substr(FIRST_NAME,2,2)
   ELSE FIRST_NAME || ' ' || LAST_NAME END) from EMPLOYEES;