Oracle SQL-视图

时间:2018-11-16 05:23:33

标签: sql oracle

这是原始代码:

    WITH counter(id, total) AS (
    SELECT stu.studentid AS "ID", count(stu.studentid) AS "Count of 
    Placement"
    FROM tn.student stu
    JOIN tn.event_result e on stu.studentid = e.studentid
    WHERE stu_sex = 'M' AND finalplacement = 1 
    GROUP BY stu.studentid )
    SELECT stu_firstname AS "First Name", stu_lastname AS "Last Name", 
    stu_sex AS "Sex", 
    stu_phonenumber AS "Phone Number", total AS "Count of Placement"
    FROM tn.student S
    JOIN counter
    ON counter.id = s.studentid
    ORDER BY total DESC;

这是通过删除stu_sex = 'm'创建的视图:

    CREATE VIEW Lubeina_view AS
    WITH counter(id, total) AS (
    SELECT stu.studentid AS "ID", count(stu.studentid) AS "Count of Placement"
    FROM tn.student stu
    JOIN tn.event_result e on stu.studentid = e.studentid
    WHERE finalplacement = 1 
    GROUP BY stu.studentid )
    SELECT stu_firstname AS "First Name", stu_lastname AS "Last Name", stu_sex 
    AS "Sex", 
    stu_phonenumber AS "Phone Number", total AS "Count of Placement"
    FROM tn.student S
    JOIN counter
    ON counter.id = s.studentid
    ORDER BY total DESC;

大家好,我正在使用Oracle SQL。我正在尝试使用VIEWS将其输出:

ORACLE SQL

我遇到的问题是我无法编写仅输出性别M或F的命令

我尝试使用此:

SELECT * 
FROM lubeina_view 
WHERE stu_sex = 'M';

但是我收到一个错误消息:

  

无效的标识符。

请帮助!我不确定哪个命令会输出图片中显示的数据

1 个答案:

答案 0 :(得分:4)

问题中的SQL代码使用stu_sex AS "Sex",稍后再使用查询:

select * from Lubeina_view where sex = 'M'

失败了,但是如果您使用过:

select * from Lubeina_view where "Sex" = 'M'

它会工作。

我认为问题是区分大小写。在Oracle中,如果定义带引号的列名,则区分大小写。我强烈建议您避免在列名称中使用空格,并且不要使用引号。

CREATE VIEW Lubeina_view
AS
WITH counter (id, total)
AS (
    SELECT
        stu.studentid          AS ID
      , COUNT( stu.studentid ) AS Count_of_Placement
    FROM tn.student stu
    JOIN tn.event_result e ON stu.studentid = e.studentid
    WHERE finalplacement = 1
    GROUP BY
        stu.studentid
)
SELECT
    stu_firstname   AS First_Name
  , stu_lastname    AS Last_Name
  , stu_sex         AS Sex
  , stu_phonenumber AS Phone_Number
  , total           AS Count_of_Placement
FROM tn.student S
JOIN counter ON counter.id = s.studentid
;

现在您可以使用where sex = 'M'where Sex = 'M'where sEx = 'M'

还敦促您不要在视图中包括order by


CREATE TABLE student(
   stu_firstname   VARCHAR(7) NOT NULL PRIMARY KEY
  ,stu_lastname    VARCHAR(5) NOT NULL
  ,stu_sex         VARCHAR(2) NOT NULL
  ,stu_phonenumber VARCHAR(9) NOT NULL
  ,total           INTEGER  NOT NULL
);
INSERT INTO student(stu_firstname,stu_lastname,stu_sex,stu_phonenumber,total) VALUES ('fiendis','plan','M','555 1234',100);
CREATE VIEW Lubeina_view
AS
SELECT
    stu_firstname   AS First_Name
  , stu_lastname    AS Last_Name
  , stu_sex         AS "Sex"
  , stu_phonenumber AS Phone_Number
  , total           AS Count_of_Placement
FROM student
;
select
*
from Lubeina_view
where sex = 'M'
;
ORA-00904: "SEX": invalid identifier
select
*
from Lubeina_view
where "Sex" = 'M'
;
FIRST_NAME | LAST_NAME | Sex | PHONE_NUMBER | COUNT_OF_PLACEMENT
:--------- | :-------- | :-- | :----------- | -----------------:
fiendis    | plan      | M   | 555 1234     |                100
CREATE VIEW Lubeina_view_2
AS
SELECT
    stu_firstname   AS First_Name
  , stu_lastname    AS Last_Name
  , stu_sex         AS Sex
  , stu_phonenumber AS Phone_Number
  , total           AS Count_of_Placement
FROM student
;
select
*
from Lubeina_view_2
where seX = 'M'
;
FIRST_NAME | LAST_NAME | SEX | PHONE_NUMBER | COUNT_OF_PLACEMENT
:--------- | :-------- | :-- | :----------- | -----------------:
fiendis    | plan      | M   | 555 1234     |                100
select
*
from Lubeina_view_2
where sEx = 'M'
;
FIRST_NAME | LAST_NAME | SEX | PHONE_NUMBER | COUNT_OF_PLACEMENT
:--------- | :-------- | :-- | :----------- | -----------------:
fiendis    | plan      | M   | 555 1234     |                100

db <>提琴here