|
篇一:MySQL語句大全集錦+加詳細解說 MYSQL查詢語句大全集錦 1:使用SHOW語句找出在服務(wù)器上當前存在什么數(shù)據(jù)庫: mysql> SHOW DATABASES; 2:2、創(chuàng)建一個數(shù)據(jù)庫MYSQLDATA mysql> CREATE DATABASE MYSQLDATA; 3:選擇你所創(chuàng)建的數(shù)據(jù)庫 mysql> USE MYSQLDATA; (按回車鍵出現(xiàn)Database changed 時說明操作成功~) 4:查看現(xiàn)在的數(shù)據(jù)庫中存在什么表 mysql> SHOW TABLES; 5:創(chuàng)建一個數(shù)據(jù)庫表 mysql> CREATE TABLE MYTABLE (name VARCHAR(20), sex CHAR(1)); 6:顯示表的結(jié)構(gòu): mysql> DESCRIBE MYTABLE; 7:往表中加入記錄 mysql> insert into MYTABLE values (‖hyq‖,‖M‖); 8:用文本方式將數(shù)據(jù)裝入數(shù)據(jù)庫表中(例如D:/mysql.txt) mysql> LOAD DATA LOCAL INFILE ―D:/mysql.txt‖ INTO TABLE MYTABLE; 9:導(dǎo)入.sql文件命令(例如D:/mysql.sql) mysql>use database; mysql>source d:/mysql.sql; 10:刪除表 mysql>drop TABLE MYTABLE; 11:清空表 mysql>delete from MYTABLE; 12:更新表中數(shù)據(jù) mysql>update MYTABLE set sex=‖f‖ where name=’hyq’; 以下是無意中在網(wǎng)絡(luò)看到的使用MySql的管理心得, 在windows中MySql以服務(wù)形式存在,在使用前應(yīng)確保此服務(wù)已經(jīng)啟動,未啟動可用net start mysql命令啟動。而Linux中啟動時可用―/etc/rc.d/init.d/mysqld start‖命令,注意啟動者應(yīng)具有管理員權(quán)限。 剛安裝好的MySql包含一個含空密碼的root帳戶和一個匿名帳戶,這是很大的安全隱患,對于一些重要的應(yīng)用我們應(yīng)將安全性盡可能提高,在這里應(yīng)把匿名帳戶刪除、 root帳戶設(shè)置密碼,可用如下命令進行: use mysql; delete from User where User=‖; update User set Password=PASSWORD(’newpassword’) where User=’root’; 如果要對用戶所用的登錄終端進行限制,可以更新User表中相應(yīng)用戶的Host字段,在進行了以上更改后應(yīng)重新啟動數(shù)據(jù)庫服務(wù),此時登錄時可用如下類似命令: mysql -uroot -p; mysql -uroot -pnewpassword; mysql mydb -uroot -p; mysql mydb -uroot -pnewpassword; 上面命令參數(shù)是常用參數(shù)的一部分,詳細情況可參考文檔。此處的mydb是要登錄的數(shù)據(jù)庫的名稱。 在 進行開發(fā)和實際應(yīng)用中,用戶不應(yīng)該只用root用戶進行連接數(shù)據(jù)庫,雖然使用root用戶進行測試時很方便,但會給系統(tǒng)帶來重大安全隱患,也不利于管理技 術(shù)的提高。我們給一個應(yīng)用中使用的用戶賦予最恰當?shù)臄?shù)據(jù)庫權(quán)限。如一個只進行數(shù)據(jù)插入的用戶不應(yīng)賦予其刪除數(shù)據(jù)的權(quán)限。MySql的用戶管理是通過 User表來實現(xiàn)的,添加新用戶常用的方法有兩個,一是在User表插入相應(yīng)的數(shù)據(jù)行,同時設(shè)置相應(yīng)的權(quán)限;二是通過GRANT命令創(chuàng)建具有某種權(quán)限的用 戶。其中GRANT的常用用法如下: grant all on mydb.* to NewUserName@HostName identified by ―password‖ ; grant usage on *.* to NewUserName@HostName identified by ―password‖; grant select,insert,update on mydb.* to NewUserName@HostName identified by ―password‖; grant update,delete on mydb.TestTable to NewUserName@HostName identified by ―password‖; 若要給此用戶賦予他在相應(yīng)對象上的權(quán)限的管理能力,可在GRANT后面添加WITH GRANT OPTION選項。而對于用插入User表添加的用戶,Password字段應(yīng)用PASSWORD 函數(shù)進行更新加密,以防不軌之人竊看密碼。 對于那些已經(jīng)不用的用戶應(yīng)給予清除,權(quán)限過界的用戶應(yīng)及時回收權(quán)限,回收權(quán)限可以通過更新User表相應(yīng)字段, 也可以使用REVOKE操作。 下面給出本人從其它資料獲得的對常用權(quán)限的解釋: 全局管理權(quán)限: FILE: 在MySQL服務(wù)器上讀寫文件。 PROCESS: 顯示或殺死屬于其它用戶的服務(wù)線程。 RELOAD: 重載訪問控制表,刷新日志等。 SHUTDOWN: 關(guān)閉MySQL服務(wù)。 數(shù)據(jù)庫/數(shù)據(jù)表/數(shù)據(jù)列權(quán)限: ALTER: 修改已存在的數(shù)據(jù)表(例如增加/刪除列)和索引。 CREATE: 建立新的數(shù)據(jù)庫或數(shù)據(jù)表。 DELETE: 刪除表的記錄。 DROP: 刪除數(shù)據(jù)表或數(shù)據(jù)庫。 INDEX: 建立或刪除索引。 INSERT: 增加表的記錄。 SELECT: 顯示/搜索表的記錄。 UPDATE: 修改表中已存在的記錄。 特別的權(quán)限: ALL: 允許做任何事(和root一樣)。 USAGE: 只允許登錄–其它什么也不允許做。 一、 簡單查詢 簡單的Transact-SQL查詢只包括選擇列表、FROM子句和WHERE子句。它們分別說明所查詢列、查詢的表或視圖、以及搜索條件等。 例如,下面的語句查詢testtable表中姓名為―張三‖的nickname字段和email字段。 復(fù)制內(nèi)容到剪貼板 代碼:SELECT `nickname`,`email`FROM `testtable` WHERE `name`=`張三` (一) 選擇列表 選擇列表(select_list)指出所查詢列,它可以是一組列名列表、星號、表達式、變量(包括局部變量和全局變量)等構(gòu)成。 1、選擇所有列 例如,下面語句顯示testtable表中所有列的數(shù)據(jù): 復(fù)制內(nèi)容到剪貼板 代碼:SELECT * FROM testtable 2、選擇部分列并指定它們的顯示次序 查詢結(jié)果集合中數(shù)據(jù)的排列順序與選擇列表中所指定的列名排列順序相同。 例如: 復(fù)制內(nèi)容到剪貼板 代碼:SELECT nickname,email FROM testtable 3、更改列標題 在選擇列表中,可重新指定列標題。定義格式為: 列標題=列名 列名 列標題 如果指定的列標題不是標準的標識符格式時,應(yīng)使用引號定界符,例如,下列語句使用漢字顯示列標題: 復(fù)制內(nèi)容到剪貼板 代碼:SELECT 昵稱=nickname,電子郵件=email FROM testtable 4、刪除重復(fù)行 SELECT語句中使用ALL或DISTINCT選項來顯示表中符合條件的所有行或刪除其中重復(fù)的數(shù)據(jù)行,默認為ALL。使用DISTINCT選項時,對于所有重復(fù)的數(shù)據(jù)行在SELECT返回的結(jié)果集合中只保留一行。 5、限制返回的行數(shù) 使用TOP n [PERCENT]選項限制返回的數(shù)據(jù)行數(shù),TOP n說明返回n行,而TOP n PERCENT時,說明n是 篇二:Mysql查詢語句大全 Mysql查詢語句大全 1.兩表之間的查詢,例如:查詢員工表中部門號與部門表中部門號相等 select * from tb_emp ,tb_dept where tb_emp.deptno=tb_dept.deptno; (這是同時顯示兩張表中相等的depton 所有字段)(tb_emp ,tb_dept這都是表名) 2.select tb_e.deptno from tb_e, tb_d where tb_e.deptno=tb_d.deptno; (這是只顯示員工表中的tb_e.deptno,并且條件是員工表中部門號與部門表中部門號相等) 3.給字段取別名 select product_price*12 totsl_product_price from productinfo; 等價select product_price*12 from productinfo; 也可以這樣寫 select product_price*12 totsl product_price from productinfo; 有特殊的字符時用雙引號的方法,(特殊字符是:中文,日文,分號等)(totsl product_price是 product_price*12) ****** 0 和空 還有空格不是一個概念 例如: select * from emp where description is null; select * from emp where description =0; select * from emp where description =`空格`; 查詢的結(jié)果都市不一樣的。 distinct 關(guān)鍵字可以查詢結(jié)果中清除重復(fù)的行,他的作用范圍是后面的所有字段的組合; 例如: select distinct deptno ,deptname from emp where deptno=23; totsl_product_price是product_price的別名; select ename, sal*12 as `年薪`from emp;別名的作用是 讓查詢出來的結(jié)果可以讓別人(外行)看了一目了然的作用上面是針對產(chǎn)品價格計算出總金額,價格*12,也就是對字段值進行加減乘除, *****任何含有空值的表達式計算后的值都是空值。( 空值+20=空值,等等) 不等值查詢(mysql兩者都支持) select * from productinfo where product_id!=33; oracl的不等值查詢 select * from productinfo where product_id>>`33`; 小于查詢 select * from productinfo where product_id>`33`; 大于查詢 select * from productinfo where product_id>`33`; 等于查詢 select * from productinfo where product_id=`33`; 在一定值范圍內(nèi)查詢 例如 1000--5000的查詢 select ename, sal as `月薪`from emp where sal>=1000 and s al>=5000; 在兩個值之間的查詢 between...... and (包含最小值和最大值) select ename, sal as `月薪`from emp where sal between 1000 and 5000; in 查詢在什么里面 示例 :在emp表中查詢部門編號為25 25 20 這三個值中的 某些字段 select ename, sal,deptno from emp where deptno in (25,26,20); not in 剛好與上面的相反 like 是做模糊查詢使用 and 表示要滿足所有條件 例如: select ename, sal,deptno from emp where ename like `%ff` and deptname=`市場部`; or 查詢 表示只要滿足其中一個條件就行,例如 select ename, sal,deptno from emp where ename like `%ff` or deptname=`市場部`; not in 取反的意思 表示不包含優(yōu)先級 級別 (可以用括號提高優(yōu)先級別) 排序用的是 order by 降序 desc 默認是升序 注意 排序時 排序的字段只能是int 類型否則排序的效果會出現(xiàn)不理想的結(jié)果 */*****可以在這樣做 sal是varchar 對sal排序時: select ename as `姓名`, sal as`月薪` from emp order by sal+0; 例如:自己建表的時候,把一個字段類型創(chuàng)建為 varchar(2) ,其實應(yīng)該建為int(2)的。因為我只允許輸出數(shù)字。這本來也沒什么,無非就是占點空間,懶得改了。但是今天在后臺發(fā)現(xiàn)排序有問題。于是,沒辦法,改之。下面簡單說一下MySQL的varchar排序問題,引以為戒。 下面,我從數(shù)據(jù)庫里面以server_id排一下序,大家來看一下排序后的結(jié)果: select server_id from cardserver where game_id = 1 order by server_id desc limit 10; +-----------+ | server_id | +-----------+ | 8| | 7| | 6| | 5| | 4| | 3| | 2| | 10 | | 1| +-----------+ 很明顯,我想要的結(jié)果應(yīng)該是 10,8,7,6,5 這樣的。但是這個10排在了2的后面。按照字符串來排的。其實我是想把它當做數(shù)值來排。 手動轉(zhuǎn)換類型: 用下面的方法就可以了,使server_id+0之后再排序,問題解決了。 select server_id from cardserver where game_id = 1 order by server_id+0 desc limit 10; +-----------+ | server_id | +-----------+ | 10 | | 8| | 7| | 6| | 5| | 4| | 3| | 2| | 1| +-----------+ 使用MySQL函數(shù)CAST/CONVERT: mysql為我們提供了兩個類型轉(zhuǎn)換函數(shù):CAST和CONVERT,現(xiàn)成的東西我們怎能放過, CAST() 和CONVERT() 函數(shù)可用來獲取一個類型的值,并產(chǎn)生另一個類型的值。 這個類型 可以是以下值其中的一個: BINARY[(N)] CHAR[(N)] DATE DATETIME DECIMAL SIGNED [INTEGER] TIME UNSIGNED [INTEGER] 所以我們也可以用CAST解決問題: select server_id from cardserver where game_id = 1 order by CAST(server_id as SIGNED) desc limit 10; 也可以使用CONVERT來搞定此問題: select server_id from cardserver where game_id = 1 order by CONVERT(server_id,SIGNED) desc limit 10; 在productinfo表中查詢 update_time ,create_time這兩個字段,條件是product_id=`33`; select update_time ,create_time from productinfo where product_id=`33`; 在兩個值之間的查詢 between...... and select ename, sal as `月薪`from emp where sal between 1000 and 5000; 插入數(shù)據(jù)時一定要注意字段的類型 int型不用加單引號,varchar 型的要加上單引號 例如: insert into emp (deptno,ename ,sex, eemail ,deptname ,sal) values (30,`luo1`,`nan`,`123456`,`開發(fā)部`,`3000`); auto_increment 自動增長 description 描述, 修改列的類型 alter table 表名 modify 列名 列類型 修改列名 alter table 表名 change 舊列名 新列名 列類型 alter table emp change name ename varchar(20); 增加列 alter table 表名 add column 字段名(列名) 列類型 (column可以有和無 標準的應(yīng)該有 column ) alter table emp add column moblie int (11); alter table 表名 add column 字段名(列名) 列類型 after/befor 列名(字段名) 刪除列 把上面的add改為drop 就行了; 修改表名 alter table 表名 rename 新表名; alter table emp rename tb_emp; rename table 表名 to新表名 rename tabletb_emp to emp; 刪除 表中的某一個字段 (注意 刪除此字段時 此字段一低昂要是不關(guān)聯(lián)的字段,否則刪除失敗,刪除成功 數(shù)據(jù)也伴隨著刪除了) 語法是: alter table 表名 drop 字段名; alter table emp drop eemail; **** 刪除表中的莫一條記錄用的時候 delete 語句 例如 :delete from emp where id =40; ****** 更新某一條記錄的數(shù)據(jù)時用 update語句 update ...set 例如; update emp set ename=`kkkk22` where id=20; 更新多個字段的值 update emp set ename =`222255`,sex=`女`where id=10; 約束。 check 約束在mysql中不起作用; >= ,=> ,= >xAND>x(不等于x的查詢)where 的條件 Incorrect integer value: `` for column `sal` at row 10 函數(shù),單行函數(shù) ,字段值的函數(shù)單行函數(shù): concat(str1,str2,....) 連接函數(shù)字段值的函數(shù)select now(); 插入 時間數(shù)據(jù) insert into emp (birth) values ( now()); 插入時間 可以用時間函數(shù),也可以手動插入,手動插入: insert into emp (birth,birthday,hierday)values(`1999-02-06`,`19 98-03-02`,`2001-05-26`);插入的時間要在單引號內(nèi)。 insert into emp (birth,birthday,hierday)values(`19990206`,`19980302`,`20010526`);這樣也行。 insert into emp (birth,birthday,hierday)value(`1999/02/06`,`199 8/05/02`,`2001/05/06`);這也行 **** 注意 函數(shù)后面要緊跟著括號 ,不能有空格號; 常用的數(shù)據(jù)類型是 int ,char ,varchar, 時間的是:date,datetime,等 聚合函數(shù)是對一組值進行計算,并返回單個值,也叫組合函數(shù),除了count()以外,聚合函數(shù)都會忽略null值。 下列是聚合函數(shù)(五個:avg,sum,max,min,count) select avg(sal) from emp; 對emp表中的sal求平均值。 select sum(sal) from emp; 求和 select count(sal) from emp; 求總共的行數(shù) select max(sal) from emp; select min(sal) from emp; select avg(sal) as `工資平均值`,max(sal) as `最高工資`,min(sal) as`最低工資`,sum(sal) as`工資總和`, count(sal) as `總共的記錄` from emp; ****** count 不統(tǒng)計null,統(tǒng)計的事記錄數(shù); select count(*) from emp; 星號是統(tǒng)計所有的記錄。 ifnull(sal,0)是如果有值就取第一個sal,否則就取零。 例如: select avg(ifnull(sal,0)) from emp; 分組統(tǒng)計:Group by 子句是真正作用在于與各種聚合函數(shù)配合使用。它是用來查詢出來的數(shù)據(jù)進行分組。 分組的含義是:把該列具有相同值的多條記錄當成一條記錄處理,最后只輸出一條記錄。分組函數(shù)忽略空值。 如果需要排序在用order by 子句。 當查詢時一定要注意 select deptno, avg(ifnull(sal,0)) from emp; 這個語句是錯誤的,因為deptno沒有出現(xiàn)在聚合函數(shù)中,也沒有出現(xiàn)在 Group by子句中,后面應(yīng)該跟 Group by 子句; select deptno, avg(sal) from emp group by deptno; 查詢每一個部門的平均工資。 select deptno, count(*) from emp group by deptno; 查詢每一個部門的總數(shù) 篇三:經(jīng)典MYSQL語句 SQL分類: DDL—數(shù)據(jù)定義語言(Create,Alter,Drop,DECLARE) DML—數(shù)據(jù)操縱語言(Select,Delete,Update,Insert) DCL—數(shù)據(jù)控制語言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,簡要介紹基礎(chǔ)語句: 1、說明:創(chuàng)建數(shù)據(jù)庫 Create DATABASE database-name 2、說明:刪除數(shù)據(jù)庫 drop database dbname 3、說明:備份sql server --- 創(chuàng)建 備份數(shù)據(jù)的 device USE master EXEC sp_addumpdevice `disk`, `testBack`, `c:mssql7backupMyNwind_1.dat` --- 開始 備份 BACKUP DATABASE pubs TO testBack 4、說明:創(chuàng)建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根據(jù)已有的表創(chuàng)建新表: A:create table tab_new like tab_old (使用舊表創(chuàng)建新表) B:create table tab_new as select col1,col2? from tab_old definition only 5、說明:刪除新表 drop table tabname 6、說明:增加一個列 Alter table tabname add column col type 注:列增加后將不能刪除。DB2中列加上后數(shù)據(jù)類型也不 能改變,唯一能改變的是增加varchar類型的長度。 7、說明:添加主鍵: Alter table tabname add primary key(col) 說明:刪除主鍵: Alter table tabname drop primary key(col) 8、說明:創(chuàng)建索引:create [unique] index idxname on tabname(col?.) 刪除索引:drop index idxname 注:索引是不可更改的,想更改必須刪除重新建。 9、說 明:創(chuàng)建視圖:create view viewname as select statement 刪除視圖:drop view viewname 10、說明:幾個簡單的基本的sql語句 選擇:select * from table1 where 范圍 插入:insert into table1(field1,field2) values(value1,value2) 刪除:delete from table1 where 范圍 更新:update table1 set field1=value1 where 范圍 查找:select * from table1 where field1 like ’%value1%’ ---like的語 排序:select * from table1 order by field1,field2 [desc] 總數(shù):select count * as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1 11、說明:幾個高級查詢運算詞 A: UNION 運算符 UNION 運算符通過組合其他兩個結(jié)果表(例如 TABLE1和 TABLE2)并消去表中任何重復(fù)行而派生出一個結(jié)果表。當 ALL 隨 UNION 一起使用時(即 UNION ALL),不消除重復(fù)行。兩種情況下,派生表的每一行不是來自 TABLE1 就是來自 TABLE2。 B: EXCEPT 運算符 EXCEPT 運算符通過包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重復(fù)行而派生出一個結(jié)果表。當 ALL 隨 EXCEPT 一起使用時 (EXCEPT ALL),不消除重復(fù)行。 C: INTERSECT 運算符 INTERSECT 運算符通過只包括 TABLE1 和 TABLE2 中都有的行并消除所有重復(fù)行而派生出一個結(jié)果表。當 ALL 隨 INTERSECT 一起使用時 (INTERSECT ALL),不消除重復(fù)行。 注:使用運算詞的幾個查詢結(jié)果行必須是一致的。 12、說明:使用外連接 A、left outer join: 左外連接(左連接):結(jié)果集幾包括連接表的匹配行,也包括左連接表的所有行。 SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c B:right outer join: 右外連接(右連接):結(jié)果集既包括連接表的匹配連接行, 也包括右連接表的所有行。 C:full outer join: 全外連接:不僅包括符號連接表的匹配行,還包括兩個連接表中的所有記錄。 其次,大家來看一些不錯的sql語句 1、說明:復(fù)制表(只復(fù)制結(jié)構(gòu),源表名:a 新表名:b) (Access 可用) 法一:select * into b from a where 1>>1 法二:select top 0 * into b from a 2、說明:拷貝表(拷貝數(shù)據(jù),源表名:a 目標表名:b) (Access可用) insert into b(a, b, c) select d,e,f from a; 3、說明:跨數(shù)據(jù)庫之間表的拷貝(具體數(shù)據(jù)使用絕對路徑) (Access可用) insert into b(a, b, c) select d,e,f from b in ‘具體數(shù)據(jù)庫’ where 條 例子:..from b in `Server.MapPath(.)data.mdb ` where.. 4、說明:子查詢(表名1:a 表名2:b) select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3) 5、說明:顯示文章、提交人和最后回復(fù)時間 select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b 6、說明:外連接查詢(表名1:a 表名2:b) select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c 7、說明:在線視圖查詢(表名1:a ) select * from (Select a,b,c FROM a) T where t.a > 1; 8、說明:between的用法,between限制查詢數(shù)據(jù)范圍時包括了邊界值,not between不包括 select * from table1 where time between time1 and time2 select a,b,c, from table1 where a not between 數(shù)值1 and 數(shù) 值2 9、說明:in 的使用方法 select * from table1 where a [not] in (‘值1’,’值2’,’值 4’,’值6’) 10、說明:兩張關(guān)聯(lián)表,刪除主表中已經(jīng)在副表中沒有的信息 delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 ) 11、說明:四表聯(lián)查問題: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where ..... 12、說明:曰程安排提前五分鐘提醒 SQL: select * from 曰程安排 where datediff(`minute`,f開始時間,getdate())>5 13、說明:一條sql 語句搞定數(shù)據(jù)庫分頁 select top 10 b.* from (select top 20 主鍵字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主鍵字段 = a.主鍵字段 order by a.排序字段 14、說明:前10條記錄 select top 10 * form table1 where 范圍 15、說明:選擇在每一組b值相同的數(shù)據(jù)中對應(yīng)的a最大的記錄的所有信息(類似這樣的用法可以用于論壇每月排行榜,每月熱銷產(chǎn)品分析,按科目成績排名,等等.) select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b) 16、說明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重復(fù)行而派生出一個結(jié)果表 (select a from tableA ) except (select a from tableB) except (select a from tableC) 17、說明:隨機取出10條數(shù)據(jù) select top 10 * from tablename order by newid() 18、說明:隨機選擇記錄 select newid() 19、說明:刪除重復(fù)記錄 Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...) 20、說明:列出數(shù)據(jù)庫里所有的表名 select name from sysobjects where type=`U` 21、說明:列出表里的所有的 select name from syscolumns where id=object_id(`TableName`) 22、說明:列示type、vender、pcs字段,以type字段排 列,case可以方便地實現(xiàn)多重選擇,類似select 中的case。 select type,sum(case vender when `A` then pcs else 0 end),sum(case vender when `C` then pcs else 0 end),sum(case vender when `B` then pcs else 0 end) FROM tablename group by type 顯示結(jié)果: type vender pcs 電腦 A 1 電腦 A 1 光盤 B 2 光盤 A 2 手機 B 3 手機 C 3 23、說明:初始化表table1 TRUNCATE TABLE table1 24、說明:選擇從10到15的記錄 select top 5 * from (select top 15 * from table order by id asc) table_別名 order by id desc 隨機選擇數(shù)據(jù)庫記錄的方法(使用Randomize函數(shù),通過SQL語句實現(xiàn)) 對存儲在數(shù)據(jù)庫中的數(shù)據(jù)來說,隨機數(shù)特性能給出上面的效果,但它們可能太慢了些。你不能要求ASP―找個隨機數(shù)‖然后打印出來。實際上常見的解決方案是建立如下所示的循環(huán): Randomize RNumber = Int(Rnd*499) +1 While Not objRec.EOF If objRec(ID) = RNumber THEN ... 這里是執(zhí)行腳本 ... end if objRec.MoveNext Wend 這很容易理解。首先,你取出1到500范圍之內(nèi)的一個隨機數(shù)(假設(shè)500就是數(shù)據(jù)庫內(nèi)記錄的總數(shù))。然后,你遍歷每一記錄來測試ID 的值、檢查其是否匹配RNumber。滿足條件的話就執(zhí)行由THEN 關(guān)鍵字開始的那一塊代碼。假如你的RNumber 等于495,那么要循環(huán)一遍數(shù)據(jù)庫花的時間可就長了。雖然500這個數(shù)字看起來大了些,但相比更為穩(wěn)固的企業(yè)解決方案這還是個小型數(shù)據(jù)庫了,后者通常在一個數(shù)據(jù)庫內(nèi)就包含了成千上萬條記錄。這時候不就死定了,采用SQL,你就可以很快地找出準確的記錄并且打開一個只包含該記錄的recordset,如下所示: Randomize RNumber = Int(Rnd*499) + 1 SQL = Select * FROM Customers Where ID = RNumber set objRec = ObjConn.Execute(SQL) Response.WriteRNumber = objRec(ID) objRec(c_email) 不必寫出RNumber 和ID,你只需要檢查匹配情況即可。只要你對以上代碼的工作滿意,你自可按需操作―隨機‖記錄。Recordset沒有包含其他內(nèi)容,因此你很快就能找到你需要的記錄這樣就大大降低了處理時間。 再談隨機數(shù) 現(xiàn)在你下定決心要榨干Random 函數(shù)的最后一滴油,那么你可能會一次取出多條隨機記錄或者想采用一定隨機范圍內(nèi)的記錄。把上面的標準Random 示例擴展一下就可以用SQL應(yīng)對上面兩種情況了。 為了取出幾條隨機選擇的記錄并存放在同一recordset內(nèi),你可以存儲三個隨機數(shù),然后查詢數(shù)據(jù)庫獲得匹配這些數(shù)字的記錄:
信息發(fā)布:廣州名易軟件有限公司 http://www.jetlc.com
|