oracle SQL语句实用范例

获取表最大ID

描述:
如果表ID为varchar类型并且表中无数据的获取方式:

select NVL(MAX(CAST(ID AS int)),0) as id from  表名

循环获取a表某列ID对应B表中需要的数据

描述:
a表callsign 查询所有的数据
例图a:
oracle SQL语句实用范例_第1张图片
描述:
b表dic_aircraft_type查询所有的数据
例图b:
oracle SQL语句实用范例_第2张图片
最总结果例图:
oracle SQL语句实用范例_第3张图片
SQL语句:

SELECT  c.id,c.CALLSIGN ,dat.name, TO_CHAR(c.CREATEDATE,'yyyy') AS CREATEDATE FROM  CALLSIGN c  
LEFT OUTER JOIN DIC_AIRCRAFT_TYPE dat ON c.AIRCREAFTID = dat.id

总结:如上图所示说明b表中查询的所有的数据都关联上A表中的name并且展示;

两个表的创建语句:


CREATE TABLE "FSA1520"."DIC_AIRCRAFT_TYPE" 
   (	"ID" VARCHAR2(32) NOT NULL ENABLE, 
	"CODE" VARCHAR2(100), 
	"NAME" VARCHAR2(100)
   ) SEGMENT CREATION IMMEDIATE 
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS" ;

COMMENT ON COLUMN "FSA1520"."DIC_AIRCRAFT_TYPE"."ID" IS 'id';
 
   COMMENT ON COLUMN "FSA1520"."DIC_AIRCRAFT_TYPE"."CODE" IS '编号';
 
   COMMENT ON COLUMN "FSA1520"."DIC_AIRCRAFT_TYPE"."NAME" IS '名称';
 
   COMMENT ON TABLE "FSA1520"."DIC_AIRCRAFT_TYPE"  IS '飞机型号';


INSERT INTO DIC_AIRCRAFT_TYPE (ID,CODE,NAME) VALUES ('1','BY-737','波音737');
INSERT INTO DIC_AIRCRAFT_TYPE (ID,CODE,NAME) VALUES ('2','A-380','A380');
INSERT INTO DIC_AIRCRAFT_TYPE (ID,CODE,NAME) VALUES ('3','BY-747','波音747');
INSERT INTO DIC_AIRCRAFT_TYPE (ID,CODE,NAME) VALUES ('4','A-340','空客340');
CREATE TABLE "FSA1520"."CALLSIGN" 
   (	"ID" VARCHAR2(100) NOT NULL ENABLE, 
	"CALLSIGN" VARCHAR2(100), 
	"CREATEDATE" TIMESTAMP (6), 
	"CREATOR" VARCHAR2(100), 
	"IS_DELETE" VARCHAR2(100), 
	"AIRCREAFTID" VARCHAR2(100)
   ) SEGMENT CREATION IMMEDIATE 
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS" ;

COMMENT ON COLUMN "FSA1520"."CALLSIGN"."ID" IS 'ID';
 
   COMMENT ON COLUMN "FSA1520"."CALLSIGN"."CALLSIGN" IS '呼号';
 
   COMMENT ON COLUMN "FSA1520"."CALLSIGN"."CREATEDATE" IS '创建时间';
 
   COMMENT ON COLUMN "FSA1520"."CALLSIGN"."CREATOR" IS '创建人';
 
   COMMENT ON COLUMN "FSA1520"."CALLSIGN"."IS_DELETE" IS '标识,0正常,1暂停';
 
   COMMENT ON COLUMN "FSA1520"."CALLSIGN"."AIRCREAFTID" IS '机型(dic_aircraft_type的ID)';
 
   COMMENT ON TABLE "FSA1520"."CALLSIGN"  IS '呼号';

INSERT INTO CALLSIGN (ID,CALLSIGN,CREATEDATE,CREATOR,IS_DELETE,AIRCREAFTID) VALUES ('1','1520',TIMESTAMP '2020-11-25 15:55:18.000000','123asf3lr7le8sdf8efs9','0','1');
INSERT INTO CALLSIGN (ID,CALLSIGN,CREATEDATE,CREATOR,IS_DELETE,AIRCREAFTID) VALUES ('2','1520',TIMESTAMP '2020-11-25 15:56:18.000000','123asf3lr7le8sdf8efs9','0','3');

你可能感兴趣的:(sql,oracle)