union 操作符用於合併兩個或多個 select 語句的結果集。
請注意,union 內部的每個 select 語句必須擁有相同數量的列。列也必須擁有相似的資料型別。同時,每個 select 語句中的列的順序必須相同。
select
column_name(s) from
table1
union
select
column_name(s) from
table2;
注釋:預設地,union 操作符選取不同的值。如果允許重複的值,請使用 union all。
select
column_name(s) from
table1
union all
select
column_name(s) from
table2;
注釋:union 結果集中的列名總是等於 union 中第乙個 select 語句中的列名。
select country from websites
union
order by country;
注釋:select country from websites
union all
order by country;
select country, name from websites
where country='cn'
union all
where country='cn'
order by country;
注:使用union命令時需要注意,只能在最後使用乙個order by命令,是將兩個查詢結果合在一起之後,再進行排序!絕對不能寫兩個order by命令。
另外,在使用order by排序時,注意兩個結果的別名保持一致,使用別名排序很方便。當然也可以使用列數。
通過 sql,您可以從乙個表複製資訊到另乙個表。
select into 語句從乙個表複製資料,然後把資料插入到另乙個新錶中。
mysql 資料庫不支援 select ... into 語句,但支援 insert into ... select 。我們可以複製所有的列插入到新錶中:當然你可以使用以下語句來拷貝表結構及資料:
create table新錶select
*from
舊表
select *
into
newtable [in
externaldb]
from
table1;
或者只複製希望的列插入到新錶中:
select
column_name(s)
into
newtable [in
externaldb]
from
table1;
建立 websites 的備份復件:
select *
into websitesbackup2016
from websites;
只複製一些列插入到新錶中:
select name, url
into websitesbackup2016
from websites;
只複製中國的**插入到新錶中:
select *
into websitesbackup2016
from websites
where country='cn';
複製多個表中的資料插入到新錶中:
select websites.name, access_log.count, access_log.date
into websitesbackup2016
from websites
left join access_log
on websites.id=access_log.site_id;
select *
into
newtable
from
table1
where 1=0;
通過 sql,您可以從乙個表複製資訊到另乙個表。
insert into select 語句從乙個表複製資料,然後把資料插入到乙個已存在的表中。
insert into select 語句從乙個表複製資料,然後把資料插入到乙個已存在的表中。目標表中任何已存在的行都不會受影響。
我們可以從乙個表中複製所有的列插入到另乙個已存在的表中:
insert into
table2
select * from
table1;
或者我們可以只複製希望的列插入到另乙個已存在的表中:
insert into
table2
(column_name(s))
select
column_name(s)
from
table1;
insert into websites (name, country)
insert into websites (name, country)
where id=1;
create database 語句用於建立資料庫。
create database
dbname;
下面的 sql 語句建立乙個名為 "my_db" 的資料庫:
create database my_db;
資料庫表可以通過 create table 語句來新增。
create table 語句用於建立資料庫中的表。
表由行和列組成,每個表都必須有個表名。
create table
table_name(
column_name1 data_type(
size),
column_name2 data_type(
size),
column_name3 data_type(
size),
....
);column_name 引數規定表中列的名稱。
data_type 引數規定列的資料型別(例如 varchar、integer、decimal、date 等等)。
size 引數規定表中列的最大長度。
現在我們想要建立乙個名為 "persons" 的表,包含五列:personid、lastname、firstname、address 和 city。
我們使用下面的 create table 語句:
create table persons (
personid int,
lastname varchar(255),
firstname varchar(255),
address varchar(255),
city varchar(255)
);personid 列的資料型別是 int,包含整數。
lastname、firstname、address 和 city 列的資料型別是 varchar,包含字元,且這些欄位的最大長度為 255 個字元。
空的 "persons" 表如下所示:
personid
lastname
firstname
address
city
SQL學習(高階二)
1 建立乙個資料庫 create database database name 2 建立資料表 create table table name column name1 datatype constraint name,column name2 datatype constraint name,da...
SQL高階模組二
1.unique unique 約束唯一標識資料庫表中的每條記錄。unique 和 primary key 約束均為列或列集合提供了唯一性的保證。primary key 約束擁有自動定義的 unique 約束。注意,每個表可以有多個 unique 約束,但是每個表只能有乙個 primary key ...
高階sql學習 with子句!!!
使用with as 語句可以為乙個子查詢語句塊定義乙個名稱,使用這個子查詢名稱可以在查詢語句的很多地方引用這個子查詢。oracle 資料庫像對待內聯檢視或臨時表一樣對待被引用的子查詢名稱,從而起到一定的優化作用。with子句是9i新增語法。你可以在任何乙個頂層的select 語句以及幾乎所有型別的子...