字段位置上的sql語句

2021-07-16 15:30:20 字數 1405 閱讀 5112

表: student

表:book

表:borrow

要求1:

查詢「計算機」專業學生在「2007-12-15」至「2008-1-8」時間段內借書的學生編號、學生名稱、圖書編號、圖書名稱、借出日期

sql語句1:

select  a.* ,b.*,c.* from (select * from borrow where t_time>'2007-12-15' and t_time

where a.stuid = b.stuid and c.bid = a.bid

sql語句2:

select stuid,(select stuname from student where stuid=borrow.stuid),bid,(select title from book where bid=borrow.bid),t_time from borrow

where stuid in (select stuid from student where major='計算機') and t_time>'2007-12-15' and t_time

兩個語句都能達到要求

sql語句2中欄位位置上的sql語句在整個sql語句中最後執行。注意:select stuname from student where stuid=borrow.stuid where後的條件鏈結,

在這裡不需要寫:select stuname from student ,borrow where stuid=borrow.stuid

原因是:整個sql語句中from後的borrow表已經作為乙個基礎表了。

要求2:

查詢目前借書但未歸還圖書的學生名稱及未還圖書數量

sql語句1:

select stu.stuname ,b.sl  from student stu,(select  * ,count(stuid) sl from borrow where b_time is null group by stuid) b  where stu.stuid=b.stuid

sql語句2:

select (select stuname from student where stuid=borrow.stuid),count(*) from borrow where b_time is null group by stuid

兩條語句都能達到效果。注意sql語句2的特點:欄位上的sql

Oracle修改字段位置

方法一 刪除表,重新建表 方法二 需要重啟資料庫 1.查詢出該表的 object id select object id from all objects where owner scott and object name tests 查詢結果為 74101 2.根據第一步查出的id,查詢表字段順序...

Hive 增添欄位並改變字段位置

一.需求 原有hive表結構如下 需要新加三個字段並且字段 statday 仍在最後的位置,目標資料表結構如下 二.hive sql實現 alter table test addcolumns country string comment 國家 city string comment 城市 stre...

全資料庫各表中查詢內容所在的字段位置

declare str varchar 100 set str ph 要搜尋的字串 declare s varchar 8000 declare tb cursor local for select s if exists select 1 from b.name where a.name like...