如何才能使此查询生效?

时间:2018-04-27 03:39:48

标签: sql database oracle

查询要求找到他们的组合涵盖了一个人追求pos_code的所有缺失知识/技能的课程集。考虑的课程集不包括三门以上的课程。如果找到多个课程集,请按照升序的顺序列出课程集(及其课程ID) 课程设定总费用。

我尝试编写的查询如下,但是在SQL Developer中引发了错误。 SQL错误:ORA-01747:user.table.column,table.column或列规范无效。这是什么原因?

SELECT c.c_code, c.title, c.level, c.description, k.ks_code, SUM(c.retail_price)
FROM course c 
INNER JOIN knowledge k 
ON c.c_code = k.c_code 
INNER JOIN requires r 
ON r.ks_code = k.ks_code
WHERE pos_code = 1
GROUP BY c.c_code, c.title, c.level, c.description, k.ks_code
ORDER BY c.retail_price;

使用的表是

CREATE TABLE course(
    c_code VARCHAR(8),
    title VARCHAR(80),
    c_level VARCHAR(20) CHECK(c_level = 'beginner' OR c_level = 'intermediate' OR c_level = 'advanced'),
    description VARCHAR(300),
    status VARCHAR(6) CHECK(status = 'expired' OR status = 'active'),
    retail_price NUMERIC(8,2) CHECK (retail_price > 0),
    PRIMARY KEY(c_code)
);

CREATE TABLE knowledge(
    ks_code VARCHAR(8),
    ks_title VARCHAR(50) NOT NULL,
    ks_description VARCHAR(300),
    ks_level VARCHAR(15) CHECK(ks_level = 'beginner' OR ks_level = 'intermediate' OR ks_level = 'advanced'),
    PRIMARY KEY(ks_code)
);

CREATE TABLE requires(
    pos_code VARCHAR(8),
    ks_code VARCHAR(8),
    PRIMARY KEY(pos_code, ks_code),
    FOREIGN KEY(pos_code) REFERENCES position ON DELETE CASCADE,
    FOREIGN KEY(ks_code) REFERENCES knowledge ON DELETE CASCADE
);

CREATE TABLE teaches(
    c_code VARCHAR(8),
    ks_code VARCHAR(8),
    PRIMARY KEY(c_code, ks_code),
    FOREIGN KEY(c_code) REFERENCES course ON DELETE CASCADE,
    FOREIGN KEY(ks_code) REFERENCES knowledge ON DELETE CASCADE
);

CREATE TABLE position(
    pos_code VARCHAR(8),
    emp_mode VARCHAR(15) CHECK(emp_mode = 'full-time' OR emp_mode = 'part-time'),
    required_skill VARCHAR(30) NOT NULL,
    pay_rate NUMERIC(8,2) CHECK(pay_rate > 0),
    pay_type VARCHAR(8) CHECK(pay_type = 'wage' OR pay_type = 'salary'),
    cate_code VARCHAR(8),
    comp_id VARCHAR(8),
    PRIMARY KEY(pos_code)
);

2 个答案:

答案 0 :(得分:0)

表格课程中没有列级别,表格知识中没有列c_code

答案 1 :(得分:0)

查询甚至没有正确构建,错过了列引用中的很多错误。

现在修复了查询,它之前的列引用错误了!

SELECT c.c_code, c.title, c.c_level, c.description, k.ks_code, SUM(c.retail_price)
FROM course c 
INNER JOIN knowledge k 
ON c.c_code = k.ks_code 
INNER JOIN requires r 
ON r.ks_code = k.ks_code
WHERE r.pos_code = 1
GROUP BY c.c_code, c.title, c.c_level, c.description, k.ks_code, c.retail_price
ORDER BY c.retail_price;

您仍需要检查以下几点:

  • 您需要检查要引用的pos_code。是来自requires还是position
  • 运行此数据查询,我刚刚检查了要执行的查询!