熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> MySQL >> 正文

MSYQL數據操作語言(DML)

2022-06-13   來源: MySQL 

  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  columncolumn

  from   tabletable

  [where condition]

  [group by …]

  [having …]

  [order by …]

  例如下面例子

  mysql> create table jokes(

  > id int unsigned not null auto_increment primary key

  > joketext text

  > jokedate date

  > )engine=myisam charset=utf;

  mysql> insert into jokes values(why not?now());

  #計算列(select 用於檢索從一個或多個表中選取出的行Select也可以被用於檢索沒有引用任何表的計算列)

  select + as total;  //結果為

  ++

  | total |

  ++

  |     |

  ++

  #從表Jokes中挑選所有的東西

  select * from  jokes;

  ++++

  | id | joketext | jokedate   |

  ++++

  |  | why not? | |

  ++++

  #選擇感興趣的列(投影)

  select id jokedate  from  jokes;

  #我們是不是可以多少顯示一點笑話正文的內容呢?(預覽)

  mysql> select idleft(joketext) as contentjokedate from jokes;

  ++++

  | id | content | jokedate   |

  ++++

  |  | why     | |

  ++++

  #統計表中記錄數

  mysql> select count(*) as records from jokes;

  ++

  | records |

  ++

  |       |

  ++

  #統計日之後錄入的笑話記錄數

  select  count(*)  from  jokes where  jokedate >= ;

  #笑話裡包含  //%表示任一字符

  mysql> select joketext as content from jokes where joketext like %not%;

  ++

  | content  |

  ++

  | why not? |

  ++

  #顯示月份的包含的笑話(組合查詢)

  select  joketext  from  where  joketext like %not% and  jokedate >=   and jokedate < ;

  分組語句

  員工表emp(empno name tel deptno sal)

  部門表dept(deptno dname addr)

  )顯示每個部門的最高工資

  select deptno max(sal) from emp  group by deptno;

  select deptnomax(sal) as 最高工資 from emp group by deptno;

  注意max()min()…函數在顯示列中出現時必須與group by一起使用

  嵌套子查詢

  )顯示每個部門收入最高的職工信息

  select *  from emp

  where sal in ( select max(sal) from emp

  group by deptno );(先按組排序後顯示一組中最大的)

  )按照工資高低顯示職工信息

  select  *  from  emp  order by sal desc;

  )limit sn

  limit子句可以被用於強制select語句返回指定的記錄數limit接受一個或兩個數字參數參數必須是一個整數常量如果給定兩個參數第一個參數指定第一個返回記錄行的偏移量第二參數指定返回記錄行的最大數目n初始記錄行的偏移量是(而不是)

  ()查看薪水最低的前位員工信息

  select * from emp order by sal limit ;

  óselect * from emp order by sal limit ;

  ()查看第到第號員工信息

  select * from emp limit ;


From:http://tw.wingwit.com/Article/program/MySQL/201311/29479.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.