時間:2015-06-28 00:00:00 來源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評論(0)
一、select語法
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name'
[CHARACTER SET charset_name]
export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]
簡化一下語法如下:
select? column1,column2,...
from?? table1,table2,...
[where condition]
[group by …]
[having …]
[order by …]
1、例如:下面例子
mysql> create table jokes(
-> id int unsigned not null auto_increment primary key,
-> joketext text,
-> jokedate date
-> )engine=myisam charset=utf8;
mysql> insert into jokes values('','why not?',now());
#計算列(select 用于檢索從一個或多個表中選取出的行。Select也可以被用于檢索沒有引用任何表的計算列)
select 1+1 as total;? //結(jié)果為2
+-------+
| total |
+-------+
|???? 2 |
+-------+
#從表Jokes中挑選所有的東西
select * from? jokes;
+----+----------+------------+
| id | joketext | jokedate?? |
+----+----------+------------+
|? 1 | why not? | 2010-04-08 |
+----+----------+------------+
#選擇感興趣的列(投影)
select id, jokedate? from? jokes;
#我們是不是可以多少顯示一點笑話正文的內(nèi)容呢?(預覽)
mysql> select id,left(joketext,3) as content,jokedate from jokes;
+----+---------+------------+
| id | content | jokedate?? |
+----+---------+------------+
|? 1 | why???? | 2010-04-08 |
+----+---------+------------+
#統(tǒng)計表中記錄數(shù)
mysql> select count(*) as records from jokes;
+---------+
| records |
+---------+
| 1 |
+---------+
#統(tǒng)計2000年1月1日之后錄入的笑話記錄數(shù)
select? count(*)? from? jokes where? jokedate >= ‘2000-01-01’;
#笑話里包含"鞋"? //%表示任一字符
mysql> select joketext as content from jokes where joketext like '%not%';
+----------+
| content? |
+----------+
| why not? |
+----------+
#顯示2000年4月份的包含"鞋"的笑話(組合查詢)
select? joketext? from? where? joketext like "%not%" and? jokedate >= "2000-04-01"? and jokedate < "2000-05-1";
2、分組語句
員工表emp(empno, name, tel, deptno, sal)
部門表dept(deptno, dname, addr)
1)顯示每個部門的最高工資
select deptno, max(sal) from emp? group by deptno;
select deptno,max(sal) as ‘最高工資’ from emp group by deptno;
注意:max()、min()…函數(shù)在顯示列中出現(xiàn)時,必須與group by一起使用
3、嵌套子查詢
1)顯示每個部門收入最高的職工信息。
select *? from emp
where sal in ( select max(sal) from emp
group by deptno );(先按組排序,后顯示一組中最大的)
2)按照工資高低顯示職工信息
select? *? from? emp? order by sal desc;
3)limit s,n
limit子句可以被用于強制select語句返回指定的記錄數(shù)。limit接受一個或兩個數(shù)字參數(shù)。參數(shù)必須是一個整數(shù)常量。如果給定兩個參數(shù),第一個參數(shù)指定第一個返回記錄行的偏移量,第二參數(shù)指定返回記錄行的最大數(shù)目n。初始記錄行的偏移量是0(而不是1)。
(1)查看薪水最低的前5位員工信息
select * from emp order by sal limit 0,5;
óselect * from emp order by sal limit 5;
?。?)查看第3到第7號員工信息
select * from emp limit 2,5;
關(guān)鍵詞標簽:MSYQL數(shù)據(jù)操作語言
相關(guān)閱讀
熱門文章 淺談JSP JDBC來連接SQL Server 2005的方法 SqlServer2005對現(xiàn)有數(shù)據(jù)進行分區(qū)具體步驟 sql server系統(tǒng)表損壞的解決方法 MS-SQL2005服務(wù)器登錄名、角色、數(shù)據(jù)庫用戶、角色、架構(gòu)的關(guān)系
人氣排行 配置和注冊O(shè)DBC數(shù)據(jù)源-odbc數(shù)據(jù)源配置教程 如何遠程備份(還原)SQL2000數(shù)據(jù)庫 SQL2000數(shù)據(jù)庫遠程導入(導出)數(shù)據(jù) SQL2000和SQL2005數(shù)據(jù)庫服務(wù)端口查看或修改 修改Sql Server唯一約束教程 SQL Server 2005降級到2000的正確操作步驟 sql server系統(tǒng)表損壞的解決方法 淺談JSP JDBC來連接SQL Server 2005的方法