国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

用SQL進(jìn)行單表查詢

2024-07-21 02:40:42
字體:
供稿:網(wǎng)友
  單表查詢是相對(duì)多表查詢而言的,指從一個(gè)數(shù)據(jù)表中查詢數(shù)據(jù)。
4.2.1 查詢所有的記錄
    在【命令編輯區(qū)】執(zhí)行輸入“select * from scott.emp”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.3所示的emp數(shù)據(jù)表所有記錄。
    【參見光盤文件】:/第4章/4.2/421.sql。
用SQL進(jìn)行單表查詢
    select * from 數(shù)據(jù)表,這里的“*”代表數(shù)據(jù)表中所有的字段。
4.2.2 查詢所有記錄的某些字段
    在【命令編輯區(qū)】輸入“select empno,ename,job from scott.emp”,然后單擊【執(zhí)行】按鈕,將顯示emp數(shù)據(jù)表的empno、ename和job字段,如圖4.4所示。
    【參見光盤文件】:/第4章/4.2/422.sql。
用SQL進(jìn)行單表查詢
    select 字段名1, 字段名2,…… from 數(shù)據(jù)表,將顯示某些特定的字段,注重這里的字段名之間的逗號(hào)是英文狀態(tài)下的逗號(hào)。
4.2.3 查詢某些字段不同記錄
    在圖4.4所示的job字段中,可以發(fā)現(xiàn)有相同的數(shù)據(jù),為了查詢有多少種不同的job,在【命令編輯區(qū)】輸入“select distinct job from scott.emp”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.5所示的結(jié)果。
    【參見光盤文件】:/第4章/4.2/423.sql。
用SQL進(jìn)行單表查詢
    select distinct 字段名 from 數(shù)據(jù)表,這里的“distinct”保留字指在顯示時(shí)去除相同的記錄,與之對(duì)應(yīng)的是“all”將保留相同的記錄,默認(rèn)為“all”。
4.2.4 單條件的查詢
    (1)在【命令編輯區(qū)】輸入“select empno,ename,job from scott.emp where job=’MANAGER’”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.6所示的字符型字段條件查詢的結(jié)果,查詢的是job為MANAGER的記錄。
    【參見光盤文件】:/第4章/4.2/424-1.sql。
用SQL進(jìn)行單表查詢
    (2)在【命令編輯區(qū)】輸入“select empno,ename,sal from scott.emp where sal<=2500”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.7所示的數(shù)字型字段條件查詢的結(jié)果,查詢的是滿足sal小于等于2500的記錄。
    【參見光盤文件】:/第4章/4.2/424-2.sql。
用SQL進(jìn)行單表查詢
    where可以指定查詢條件,假如是指定字符型字段查詢條件,形式為字段名 運(yùn)算符 '字符串';假如是指定數(shù)字型字段查詢條件,形式為字段名 運(yùn)算符 '字符串'。 單條件查詢使用的比較運(yùn)算符如表4.1所示。
    【參見光盤文件】:/第4章/4.2/table41.sql。
表4.1 比較運(yùn)算符名稱實(shí)例=(等于)select * from scott.emp where job=’MANAGER’;select * from scott.emp where sal=1100;!= (不等于)select * from scott.emp where job!=’MANAGER’;select * from scott.emp where sal!=1100;^=(不等于)select * from scott.emp where job^=’MANAGER’;select * from scott.emp where sal^=1100;<>(不等于)select * from scott.emp where job<>’MANAGER’;select * from scott.emp where sal<>1100;<(小于)select * from scott.emp where sal<2000;select * from scott.emp where job<’MANAGER’;>(大于)select * from scott.emp where sal>2000;select * from scott.emp where job>’MANAGER’;<=(小于等于)select * from scott.emp where sal<=2000;select * from scott.emp where job<=’MANAGER’;>=(大于等于)select * from scott.emp where sal>=2000;select * from scott.emp where job>=’MANAGER’;in(列表)select * from scott.emp where sal in (2000,1000,3000);select * from scott.emp where job in (’MANAGER’,’CLERK’);not in(不在列表)select * from scott.emp where sal not in (2000,1000,3000);select * from scott.emp where job not in (’MANAGER’,’CLERK’);between(介于之間)select * from scott.emp where sal between 2000 and 3000;select * from scott.emp where job between ’MANAGER’ and ’CLERK’;not between (不介于之間)select * from scott.emp where sal not between 2000 and 3000;select * from scott.emp where job not between ’MANAGER’ and ’CLERK’;like(模式匹配)select * from scott.emp where job like ’M%’;select * from scott.emp where job like ’M__’;not like (模式不匹配)select * from scott.emp where job not like ’M%’;select * from scott.emp where job not like ’M__’;Is null (是否為空)select * from scott.emp where sal is null;select * from scott.emp where job is null;is not null(是否為空)select * from scott.emp where sal is not null;select * from scott.emp where job is not null;
    like和not like適合字符型字段的查詢,%代表任意長度的字符串,_下劃線代表一個(gè)任意的字符。like ‘m%’ 代表m開頭的任意長度的字符串,like ‘m__’ 代表m開頭的長度為3的字符串。
4.2.5 組合條件的查詢
    (1)在【命令編輯區(qū)】輸入“select empno,ename,job from scott.emp where job>=’CLERK’ and sal<=2000”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.8所示的邏輯與組合查詢的結(jié)果。
    【參見光盤文件】:/第4章/4.2/425-1.sql。
用SQL進(jìn)行單表查詢
    (2)在【命令編輯區(qū)】輸入“select empno,ename,job from scott.emp where job>=’CLERK’ or sal<=2000”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.9所示的邏輯或組合查詢的結(jié)果。
    【參見光盤文件】:/第4章/4.2/425-2.sql。
用SQL進(jìn)行單表查詢
    (3)在【命令編輯區(qū)】輸入“select empno,ename,job from scott.emp where not job=’CLERK’”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.10所示的邏輯非組合查詢的結(jié)果。
    【參見光盤文件】:/第4章/4.2/425-3.sql。
用SQL進(jìn)行單表查詢 
    “not job=’CLERK’”等價(jià)于“job<>’CLERK’”。
    組合條件中使用的邏輯比較符如表4.2所示。
    【參見光盤文件】:/第4章/4.2/table42.sql。
表4.2 邏輯比較符名稱實(shí)例and(與)select * from scott.emp where job=’MANAGER’ and sal<>2000;or (或)select * from scott.emp where job!=’MANAGER’ or sal<>2000;not(非)select * from scott.emp where not job>=’MANAGER’;4.2.6 排序查詢
    在【命令編輯區(qū)】輸入“select empno,ename,job from scott.emp where job<=’CLERK’ order by job asc,sal desc”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.11所示的排序查詢的結(jié)果。
    【參見光盤文件】:/第4章/4.2/426.sql。
用SQL進(jìn)行單表查詢
    order by 可以指定查詢結(jié)果如何排序,形式為字段名 排序要害詞;asc代表升序排列,desc代表降序排列,多個(gè)排序字段之間通過逗號(hào)分割。若有where查詢條件,order by要放在where語句后面。
4.2.7 分組查詢
    分組查詢是指將查詢結(jié)果按照字段分組。
    (1)在【命令編輯區(qū)】輸入“select empno,ename,job,sal from scott.emp group by job,empno,ename,sal having sal<=2000”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.12所示的分組查詢的結(jié)果。
    【參見光盤文件】:/第4章/4.2/427-1.sql。
用SQL進(jìn)行單表查詢
    (2)在【命令編輯區(qū)】輸入“select empno,ename,job,sal from scott.emp where sal<=2000 group by job,empno,ename,sal”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.13所示的分組查詢的結(jié)果。
    【參見光盤文件】:/第4章/4.2/427-2.sql。
用SQL進(jìn)行單表查詢
    where檢查每條記錄是否符合條件,having是檢查分組后的各組是否滿足條件。having語句只能配合group by語句使用,沒有g(shù)roup by時(shí)不能使用having,但可以使用where。
4.2.8 字段運(yùn)算查詢
    可以利用幾種基本的算術(shù)運(yùn)算符來查詢數(shù)據(jù)。
    常見的+(加)、-(減)、*(乘)、/(除)4種算術(shù)運(yùn)算都可以用來查詢數(shù)據(jù)。
    在【命令編輯區(qū)】輸入“select empno,ename,sal,mgr,sal+mgr from scott.emp”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.14所示的結(jié)果。

    【參見光盤文件】:/第4章/4.2/428.sql。
用SQL進(jìn)行單表查詢
    利用算術(shù)運(yùn)算符僅僅適合多個(gè)數(shù)值型字段或字段與數(shù)字之間的運(yùn)算。
4.2.9 變換查詢顯示
    在【命令編輯區(qū)】輸入“select empno 編號(hào),ename 姓名,job 工作,sal 薪水 from scott.emp”,然后單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.15所示的結(jié)果,可以將默認(rèn)的字段名以設(shè)定的名稱顯示。
    【參見光盤文件】:/第4章/4.2/429.sql。
用SQL進(jìn)行單表查詢
    以上我們學(xué)習(xí)了對(duì)單個(gè)數(shù)據(jù)表的查詢語句。將上面這些基本的實(shí)例經(jīng)過組合,就可以完成基本的日常數(shù)據(jù)查詢?nèi)蝿?wù),接下來進(jìn)一步學(xué)習(xí)多表查詢。

上一篇:SQL概述

下一篇:用SQL進(jìn)行多表查詢

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桓台县| 丹棱县| 塔城市| 新龙县| 巴马| 明光市| 阜城县| 句容市| 永和县| 沙洋县| 长春市| 晋江市| 阳高县| 阳新县| 夏邑县| 信阳市| 关岭| 漯河市| 满城县| 龙里县| 株洲市| 中卫市| 湖南省| 剑河县| 宁都县| 筠连县| 钟祥市| 枣庄市| 遂昌县| 宁德市| 台湾省| 荥经县| 桦甸市| 资兴市| 莆田市| 龙里县| 潮安县| 高要市| 汉源县| 武冈市| 内乡县|