6

谁能帮我弄清楚为什么我会出错cms.CRIME_ID

不合法的识别符

select c.criminal_id, c.first, c.last, cms.CRIME_ID, cc.crime_code, cc.fine_amount
from criminals c join crimes cms on c.criminal_id = cms.criminal_id
join crime_charges cc using (crime_id)
order by c.first, c.last;

我知道列存在的绝对事实,我可以引用该表中除此之外的所有其他列。

该列的唯一不同之处在于它是该表的主键。

编辑:这是完整的错误和表创建脚本。

Error starting at line 1 in command:
select c.criminal_id, c.first, c.last, cms.CRIME_ID, cc.crime_code, cc.fine_amount
from criminals c join crimes cms on c.criminal_id = cms.criminal_id
join crime_charges cc using (crime_id)
order by c.first, c.last
Error at Command Line:1 Column:39
Error report:
SQL Error: ORA-00904: "CMS"."CRIME_ID": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:

 CREATE TABLE crimes
      (crime_id NUMBER(9),
       criminal_id NUMBER(6),
       classification CHAR(1),
       date_charged DATE,
       status CHAR(2),
       hearing_date DATE,
       appeal_cut_date DATE);

ALTER TABLE crimes
  MODIFY (classification DEFAULT 'U');
ALTER TABLE crimes
  ADD (date_recorded DATE DEFAULT SYSDATE);
ALTER TABLE crimes
  MODIFY (criminal_id NOT NULL);
ALTER TABLE crimes
  ADD CONSTRAINT crimes_id_pk PRIMARY KEY (crime_id);
ALTER TABLE crimes
  ADD CONSTRAINT crimes_class_ck CHECK (classification IN('F','M','O','U'));
ALTER TABLE crimes
  ADD CONSTRAINT crimes_status_ck CHECK (status IN('CL','CA','IA'));
ALTER TABLE crimes
  ADD CONSTRAINT crimes_criminalid_fk FOREIGN KEY (criminal_id)
             REFERENCES criminals(criminal_id);
ALTER TABLE crimes
  MODIFY (criminal_id NOT NULL);

EDIT2:另外,我可能应该提到,当不使用连接和仅使用常规选择语句时,我可以很好地访问该列,如以下代码示例所示:

select c.criminal_id, c.first, c.last, cms.crime_id, cc.crime_code, cc.fine_amount
from criminals c, crime_charges cc, crimes cms
where c.criminal_id = cms.criminal_id
and cms.crime_id = cc.crime_id
order by c.first, c.last;
4

4 回答 4

3

这里的问题是,当您的查询有一个USING子句时,您不能向该子句中使用的列添加限定符。因为你的查询有USING (crime_id),你不能写cms.CRIME_ID也不cc.crime_id。相反,您必须删除限定符,即只使用crime_id.

奇怪的是,当我在 Oracle 11g XE beta 上尝试这个时,我得到一个不同的错误:

SQL> 从 test1 中选择 *;

         AB
---------- ----------
         1 2

SQL> 从 test2 中选择 *;

         交流电
---------- ----------
         1 3

SQL> select t1.a, t1.b, t2.c from test1 t1 inner join test2 t2 using (a);
使用 (a) 从 test1 t1 内部连接 ​​test2 t2 中选择 t1.a, t1.b, t2.c
       *
第 1 行的错误:
ORA-25154: USING 子句的列部分不能有限定符


SQL> select a, t1.b, t2.c from test1 t1 inner join test2 t2 using (a);

         美国广播公司
---------- ---------- ----------
         1 2 3
于 2011-09-18T07:44:57.337 回答
1

您不能将限定符与“using”子句引用的列一起使用。您可以使用内部联接而不是尝试使用此查询:

select c.criminal_id, c.first, c.last, cms.CRIME_ID, cc.crime_code, cc.fine_amount
from criminals c join crimes cms on c.criminal_id = cms.criminal_id
join crime_charges cc on cc.crime_id=cms.crime_id
order by c.first, c.last;
于 2011-09-18T06:49:27.333 回答
0

try to make an alias for join statements clause:

select alias.criminal_id, alias.first, alias.last, alias.CRIME_ID, alias.crime_code, alias.fine_amount
from criminals c join crimes cms on c.criminal_id = cms.criminal_id
join crime_charges cc using (crime_id) as alias
order by alias.first, alias.last;
于 2011-09-14T04:20:24.327 回答
0

你试过以下吗?

Join On (Left Id) = (Right Id)

而不是关键字Using

于 2011-09-14T02:57:52.497 回答