Sql 连接不同大小的数字列

Sql 连接不同大小的数字列,sql,oracle,Sql,Oracle,表1的类型。mycolumn是编号(10),表2的类型是是编号(6)。我该怎么做才能加入他们而不被Oracle抛出错误?为什么您认为会出现错误?你试过了吗 SELECT * FROM table1 JOIN table2 ON table1.mycolumn = table2.mycolumn 你为什么认为你会出错?你试过了吗 SELECT * FROM table1 JOIN table2 ON table1.mycolumn = table2.mycolumn 执行此操作时没有错

表1的类型。mycolumn
编号(10)
,表2的类型是
编号(6)
。我该怎么做才能加入他们而不被Oracle抛出错误?

为什么您认为会出现错误?你试过了吗

SELECT * 
FROM table1 
JOIN table2 ON table1.mycolumn = table2.mycolumn

你为什么认为你会出错?你试过了吗

SELECT * 
FROM table1 
JOIN table2 ON table1.mycolumn = table2.mycolumn

执行此操作时没有错误:

Oracle 11g R2架构设置

create table t1 (col1 number(10));

create table t2 (col1 number(6));

insert into t1
select 1000000001 col1 from dual union all
select 1111111 col1 from dual union all
select 1 col1 from dual;

insert into t2
select 111111 col1 from dual union all
select 1 col1 from dual;

commit;

select *
from   t1 inner join t2 on (t1.col1 = t2.col1);

      COL1     COL1_1
---------- ----------
         1          1
CREATE TABLE table_a (
  num_a NUMBER(6)
);

CREATE TABLE table_b (
  num_b NUMBER(10)
);

INSERT INTO table_a VALUES ( 123456 );
INSERT INTO table_b VALUES ( 123456 );
INSERT INTO table_b VALUES ( 1234567890 );
select *
from table_a a
     join table_b b
     on a.num_a = b.num_b
查询1

create table t1 (col1 number(10));

create table t2 (col1 number(6));

insert into t1
select 1000000001 col1 from dual union all
select 1111111 col1 from dual union all
select 1 col1 from dual;

insert into t2
select 111111 col1 from dual union all
select 1 col1 from dual;

commit;

select *
from   t1 inner join t2 on (t1.col1 = t2.col1);

      COL1     COL1_1
---------- ----------
         1          1
CREATE TABLE table_a (
  num_a NUMBER(6)
);

CREATE TABLE table_b (
  num_b NUMBER(10)
);

INSERT INTO table_a VALUES ( 123456 );
INSERT INTO table_b VALUES ( 123456 );
INSERT INTO table_b VALUES ( 1234567890 );
select *
from table_a a
     join table_b b
     on a.num_a = b.num_b

create table t1 (col1 number(10));

create table t2 (col1 number(6));

insert into t1
select 1000000001 col1 from dual union all
select 1111111 col1 from dual union all
select 1 col1 from dual;

insert into t2
select 111111 col1 from dual union all
select 1 col1 from dual;

commit;

select *
from   t1 inner join t2 on (t1.col1 = t2.col1);

      COL1     COL1_1
---------- ----------
         1          1
CREATE TABLE table_a (
  num_a NUMBER(6)
);

CREATE TABLE table_b (
  num_b NUMBER(10)
);

INSERT INTO table_a VALUES ( 123456 );
INSERT INTO table_b VALUES ( 123456 );
INSERT INTO table_b VALUES ( 1234567890 );
select *
from table_a a
     join table_b b
     on a.num_a = b.num_b

执行此操作时没有错误:

Oracle 11g R2架构设置

create table t1 (col1 number(10));

create table t2 (col1 number(6));

insert into t1
select 1000000001 col1 from dual union all
select 1111111 col1 from dual union all
select 1 col1 from dual;

insert into t2
select 111111 col1 from dual union all
select 1 col1 from dual;

commit;

select *
from   t1 inner join t2 on (t1.col1 = t2.col1);

      COL1     COL1_1
---------- ----------
         1          1
CREATE TABLE table_a (
  num_a NUMBER(6)
);

CREATE TABLE table_b (
  num_b NUMBER(10)
);

INSERT INTO table_a VALUES ( 123456 );
INSERT INTO table_b VALUES ( 123456 );
INSERT INTO table_b VALUES ( 1234567890 );
select *
from table_a a
     join table_b b
     on a.num_a = b.num_b
查询1

create table t1 (col1 number(10));

create table t2 (col1 number(6));

insert into t1
select 1000000001 col1 from dual union all
select 1111111 col1 from dual union all
select 1 col1 from dual;

insert into t2
select 111111 col1 from dual union all
select 1 col1 from dual;

commit;

select *
from   t1 inner join t2 on (t1.col1 = t2.col1);

      COL1     COL1_1
---------- ----------
         1          1
CREATE TABLE table_a (
  num_a NUMBER(6)
);

CREATE TABLE table_b (
  num_b NUMBER(10)
);

INSERT INTO table_a VALUES ( 123456 );
INSERT INTO table_b VALUES ( 123456 );
INSERT INTO table_b VALUES ( 1234567890 );
select *
from table_a a
     join table_b b
     on a.num_a = b.num_b

create table t1 (col1 number(10));

create table t2 (col1 number(6));

insert into t1
select 1000000001 col1 from dual union all
select 1111111 col1 from dual union all
select 1 col1 from dual;

insert into t2
select 111111 col1 from dual union all
select 1 col1 from dual;

commit;

select *
from   t1 inner join t2 on (t1.col1 = t2.col1);

      COL1     COL1_1
---------- ----------
         1          1
CREATE TABLE table_a (
  num_a NUMBER(6)
);

CREATE TABLE table_b (
  num_b NUMBER(10)
);

INSERT INTO table_a VALUES ( 123456 );
INSERT INTO table_b VALUES ( 123456 );
INSERT INTO table_b VALUES ( 1234567890 );
select *
from table_a a
     join table_b b
     on a.num_a = b.num_b

我的意思是,如果不执行更改数据定义的操作,只需将它们连接起来,就无需执行任何操作。我希望@ughai所说的是
CAST()
,如:
CAST(table2.mycolumn number(10))
。但我认为这与本案无关。假设
table1.mycolumn=table2.column
是一个打字错误,您的意思是
table1.mycolumn=table2.mycolumn
,也就是说,您可以将mycolumn从table1分为长度6,但这不能保证您会得到正确的结果,并且将数字值6+长度转换为6将导致ORA-01438),因此只有substrI的意思是,如果不执行更改数据定义的操作,只需加入它们,就无需执行任何操作。我希望@ughai所说的是
CAST()
,如:
CAST(table2.mycolumn number(10))
。但我认为这与本案无关。假设
table1.mycolumn=table2.column
是一个打字错误,您的意思是
table1.mycolumn=table2.mycolumn
,也就是说,您可以将mycolumn从table1子串到长度6,但这不能保证您会得到正确的结果,并且将6+长度的数值强制转换为6会导致ORA-01438),因此只有substr