首頁  >  文章  >  電腦教學  >  如何使用一條SQL語句同時在A表和B表中產生一條數據

如何使用一條SQL語句同時在A表和B表中產生一條數據

WBOY
WBOY轉載
2024-01-25 09:00:06982瀏覽

如何使用一條SQL語句同時在A表和B表中產生一條數據

如何建立一個sql語句使在a表中產生一條資料同時b表也產生一

使用迴圈語句,可以插入任意的資料。

參考程式碼:

delete from whilestu1;

commit;

DECLARE

#num1 number;

maxstuid number;

age number;

birthday date;

begin

num1:=1;

WHILE num1--取得最大的stuid

select max(stuid) 1 into maxstuid from whilestu1;

if maxstuid is null then

maxstuid:=1;

end if;

birthday:=sysdate-ROUND(DBMS_RANDOM.VALUE(300,600),0);

age:=ROUND(DBMS_RANDOM.VALUE(18,40),0);

--插入資料

insert into whilestu1(stuid,stuName,age,birthday)

values (maxstuid,'學員'||cast(maxstuid as varchar2(50)),age,birthday);

commit;

num1:=num1 1;

END LOOP;

end;

/

SQL在查詢結果中選取某一條資料

-- 用的啥資料庫也不寫清楚。 。 。

-- MS sql server2005以上,ORACLE

select * from (

select row_number() over ( order by starttime asc) as rownum,* from steriworkrecord where starttime between '2013-11-1' and '2013-12-31'

) a

where rownum between 2 and 10-- 【注意( order by starttime asc)是你排序的方式asc升序,desc降序】

================================================= =========

-- ORACLE還可以

select * from (

select rownum as n,* from steriworkrecord

#where starttime between '2013-11-1' and '2013-12-31'

) a

where a.n between 2 and 10========================================= =================

#-- MYSQL,postgreSQL似乎只能標量子查詢

SELECT *FROM (

SELECT a.*,(

SELECT count(*) FROM steriworkrecordb WHERE b.ID

from steriworkrecorda

) ts

where ts.n between 2 and 10

-- 【注意b.ID

-- 程式碼都忙你實際測試了ok

如何用SQL語句取出資料庫中的特定一條資料

透過查詢語句select * from user where id=1

我不知道你這個username指的是不是字段,如果是要取出表中某個字段的值。

可以透過select 欄位名稱1,欄位名稱2 ... from user where id=1。

-- MS sql server2005以上,ORACLE

select * from (

select row_number() over ( order by starttime asc) as rownum,* from steriworkrecord

#where starttime between '2013-11-1' and '2013-12-31'

) a

where rownum between 2 and 10

-- 【注意( order by starttime asc)是你排序的方式asc升序,desc降序】

-- ORACLE還可以

select * from (

select rownum as n,* from steriworkrecord

##where starttime between '2013-11-1' and '2013-12-31'

) a

where a.n between 2 and 10

-- MYSQL,postgreSQL似乎只能標量子查詢

SELECT *FROM (

SELECT a.*,(

SELECT count(*) FROM steriworkrecordb WHERE b.ID

from steriworkrecorda

) ts

where ts.n between 2 and 10 ###-- 【注意b.ID

以上是如何使用一條SQL語句同時在A表和B表中產生一條數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:docexcel.net。如有侵權,請聯絡admin@php.cn刪除