MY SQL語句常用集合

2021-09-20 14:16:23 字數 3618 閱讀 8192

1個資料庫通常包含乙個或多個表。每個表由乙個名字標識

1.select - 從資料庫表中獲取資料

update - 更新資料庫表中的資料

delete - 從資料庫表中刪除資料

insert into - 向資料庫表中插入資料

2.create database - 建立新資料庫

alter database - 修改資料庫

create table - 建立新錶

alter table - 變更(改變)資料庫表

drop table - 刪除表

create index - 建立索引(搜尋鍵)

drop index - 刪除索引

3.select 列名稱 from 表名稱

4.select * from 表名稱

:星號(*)是選取所有列的快捷方式

5.select distinct 列名稱 from 表名稱//去除重複

6.where 子句用於規定選擇的標準。

select 列名稱 from 表名稱 where 列 運算子 值

使用 where 子句

select * from persons where city=』beijing』

使用 and 來顯示所有姓為 「carter」 並且名為 「thomas」 的人:

select * from persons where firstname=』thomas』 and lastname=』carter』

使用 or 來顯示所有姓為 「carter」 或者名為 「thomas」 的人:

select * from persons where firstname=』thomas』 or lastname=』carter』

7.我們也可以把 and 和 or 結合起來(使用圓括號來組成複雜的表示式):

select * from persons where (firstname=』thomas』 or firstname=』william』)

and lastname=』carter』

8.order by 語句用於根據指定的列對結果集進行排序。

select company, ordernumber from orders order by company,

select company, ordernumber from orders order by company desc

//預設為公升序desc為降序

9.insert into 語句

insert into 表名稱 values (值1, 值2,….)

(插入的值的順序要和表字段對應)

10update 語句用於修改表中的資料。

update 表名稱 set 列名稱 = 新值 where 列名稱 = 某值

11.delete 語句用於刪除表中的行

delete from 表名稱 where 列名稱 = 值

drop table 表名,刪表。

11.top 子句用於規定要返回的記錄的數目。

select top number | percent column_name(s)

from table_name

mysql 和 oracle 中的 sql select top 是等價的

「persons」 表中選取頭兩條記錄。

現在,我們希望從上面的 「persons」 表中選取 50% 的記錄

select top 50 percent * from persons

12.like 操作符用於在 where 子句中搜尋列中的指定模式。

select * from persons

where city like 『n%』

n%表示查詢所有以n開頭的%n表示以n結尾%n表示中帶n的%

使用 _ 萬用字元

上面的 「persons」 表中選取名字的第乙個字元之後是 「eorge」 的人

select * from persons

where firstname like 『_eorge』

接下來,我們希望從 「persons」 表中選取的這條記錄的姓氏以 「c」 開頭,然後是乙個任意字元,然後是 「r」,然後是任意字元,然後是 「er」:

select * from persons

where lastname like 『c_r_er』

我們希望從上面的 「persons」 表中選取居住的城市不以 「a」 或 「l」 或 「n」 開頭的人:

select * from persons

where city like 『[!aln]%』

13in 操作符允許我們在 where 子句中規定多個值。

現在,我們希望從上表中選取姓氏為 adams 和 carter 的人:

select * from persons

where lastname in (『adams』,』carter』)

操作符 between … and 會選取介於兩個值之間的資料範圍。這些值可以是數值、文字或者日期

如需以字母順序顯示介於 「adams」(包括)和 「carter」(不包括)之間的人,請使用下面的 sql

select * from persons

where lastname

between 『adams』 and 『carter』

顯示範圍之外的人,請使用 not 操作符:

select * from persons

where lastname

not between 『adams』 and 『carter』

14.使用表名稱別名

select po.orderid, p.lastname, p.firstname

from persons as p, product_orders as po

where p.lastname=』adams』 and p.firstname=』john』

使用as關鍵字可以給表器小名 orcl的as可以省略不寫mysql忘了也沒時間去試了。

15.sql join 用於根據兩個或多個表中的列之間的關係,從這些表中查詢資料。

select persons.lastname, persons.firstname, orders.orderno

from persons

inner join orders

or persons.id_p = orders.id_p

order by persons.lastname

join: 如果表中有至少乙個匹配,則返回行

left join: 即使右表中沒有匹配,也從左表返回所有的行

right join: 即使左表中沒有匹配,也從右表返回所有的行

full join: 只要其中乙個表中存在匹配,就返回行

常用mysql語句 常用MySql語句

新建資料表 drop table if exists ga game way create table ga game way id int 11 unsigned not null auto increment comment id primary key id using btree,主鍵 un...

常用SQL語句集合

1 說明 複製表 只複製結構,源表名 a 新錶名 b access可用 法一 select into b from a where 1 1 法二 select top 0 into b from a 2 說明 拷貝表 拷貝資料,源表名 a 目標表名 b access可用 insert into b ...

常用MySQL語句

1.庫的操作 2.表的操作 3.索引的操作 4.檢視的操作 5.資料的操作 建立資料庫 create database database name 檢視資料庫 show databases 選擇資料庫 use database name 刪除資料庫 drop database database na...