當前位置:首頁 » 編程語言 » 用舊表查詢sql語句
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

用舊表查詢sql語句

發布時間: 2022-08-10 10:10:11

1. sql多表查詢語句怎麼寫

SQL多表查詢語句的步驟如下:

我們需要准備的材料分別是:電腦、sql查詢器。

1、首先,打開sql查詢器,連接上相應的資料庫表,例如m1表和m2表。

2. 怎樣用SQL語句查詢一個資料庫中的所有表

1、打開Microsoft SQL Server 2012,選中需要查詢所有表的資料庫。

3. 搜集SQL常用的操作語句

結構化查詢語言(Structured Query Language)簡稱SQL(發音:/ˈes kjuː ˈel/ "S-Q-L"),是一種特殊目的的編程語言,是一種資料庫查詢和程序設計語言,用於存取數據以及查詢、更新和管理關系資料庫系統;同時也是資料庫腳本文件的擴展名。

一、插入(復制)表數據

1、INSERT INTO 語句:

(1)插入新的一行數

[sql] view plain ;

INSERT INTO Persons VALUES ('Gates', 'Bill', 'Xuanwumen 10', 'Beijing');

(2)在指定的列中插入數據

[sql] view plain ;

INSERT INTO Persons (LastName, Address) VALUES ('Wilson', 'Champs-Elysees');

2、SQL SELECT INTO 語句可用於創建表的備份復件

(1)在建表時復制所有數據

[sql] view plain ;

create table userinfo_new as select * from userinfo;

(2)在建表時復制部分數據

[sql] view plain ;

create table userinfo_new1 as select id,username from userinfo;

(3)在添加時復制所有數據

[sql] view plain ;

insert into userinfo_new select * from userinfo;

(4)在添加時復制部分數據

[sql] view plain ;

insert into userinfo_new(id,username) select id,username from userinfo;

二、修改表數據

Update 語句

(1)無條件更新

[sql] view plain ;

update userinfo set userpwd='111',email='[email protected]';

(2)有條件更新

[sql] view plain ;

update userinfo set userpwd='123456' where username='xxx';

三、刪除表數據

1、DELETE 語句

(1)無條件刪除

[sql]view plain;

daletefromuserinfo;

(2)有條件刪除

[sql]view plain;

='yyy';

四、查詢表數據

1、SELECT 語句:

(1)查詢所有欄位

[sql] view plain ;

select * from users;

(2)查詢指定欄位

[sql] view plain ;

select username,salary from users;

2、SELECT DISTINCT 語句

從 Company" 列中僅選取唯一不同的值,需要使用 SELECT DISTINCT 語句:

[sql] view plain ;

SELECT DISTINCT Company FROM Orders;

4. SQL的語句等一些操作

select *
from 學生表
where 年齡 >(select 年齡 from 學生表 where 姓名 = 'tan')
查詢的話可以嵌套查詢,select語句是sql裡面最復雜的一條命令。
「是不是都能用表+欄位名+欄位這樣拿來帶?」這個認識是錯誤的,只能 資料庫名.表名.欄位名 來獲取欄位

5. sql 如何查詢歷史操作語句

打開Log Explorer -> Attach Log File -> 選擇SQL Server伺服器和登陸方式 -> Connect -> 在Database Name中選擇資料庫 -> Attach-> 左面對話框中Browse-> View Log-> 就可以看到log記錄了
想恢復的話: 右鍵Log記錄 Undo Transation-> 選擇保存文件名和路徑-> 然後打開該文件到查詢分析器里執行 T-sql代碼就可以了
例如 如果Log是delete table where ...的話,生成的文件代碼就是insert table .... 然後將此insert table的代碼放到查詢分析器里執行.就可以恢復數據.
Log Explorer使用的一個問題
1)對資料庫做了完全 差異 和日誌備份 備份時選用了刪除事務日誌中不活動的條目 再用Log Explorer打試圖看日誌時 提示No log recorders found that match the filter,would you like to view unfiltered data 選擇yes 就看不到剛才的記錄了
如果不選用了刪除事務日誌中不活動的條目 再用Log Explorer打試圖看日誌時,就能看到原來的日誌
2)修改了其中一個表中的部分數據,此時用Log Explorer看日誌,可以作日誌恢復
3)然後恢復備份,(注意:恢復是斷開Log Explorer與資料庫的連接,或連接到其他數據上, 否則會出現資料庫正在使用無法恢復) 恢復完後,再打開Log Explorer 提示No log recorders found that match the filter,would you like to view unfiltered data 選擇yes 就看不到剛才在2中修改的日誌記錄,所以無法做恢復.

6. 誰能給下SQL語句查詢大全,謝謝!

日誌清除
SET NOCOUNT ON DECLARE @LogicalFileName sysname, @MaxMinutes INT, @NewSize INT USE tablename -- 要操作的資料庫名 Select @LogicalFileName = 'tablename_log', -- 日誌文件名 @MaxMinutes = 10, -- Limit on time allowed to wrap log. @NewSize = 1 -- 你想設定的日誌文件的大小(M) -- Setup / initialize DECLARE @OriginalSize int Select @OriginalSize = size FROM sysfiles Where name = @LogicalFileName Select 'Original Size of ' + db_name() + ' LOG is ' + CONVERT(VARCHAR(30),@OriginalSize) + ' 8K pages or ' + CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 'MB' FROM sysfiles Where name = @LogicalFileName Create TABLE DummyTrans (DummyColumn char (8000) not null) DECLARE @Counter INT, @StartTime DATETIME, @TruncLog VARCHAR(255) Select @StartTime = GETDATE(), @TruncLog = 'BACKUP LOG ' + db_name() + ' WITH TRUNCATE_ONLY' DBCC SHRINKFILE (@LogicalFileName, @NewSize) EXEC (@TruncLog) -- Wrap the log if necessary. WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) -- time has not expired AND @OriginalSize = (Select size FROM sysfiles Where name = @LogicalFileName) AND (@OriginalSize * 8 /1024) > @NewSize BEGIN -- Outer loop. Select @Counter = 0 WHILE ((@Counter < @OriginalSize / 16) AND (@Counter < 50000)) BEGIN -- update Insert DummyTrans VALUES ('Fill Log') Delete DummyTrans Select @Counter = @Counter + 1 END EXEC (@TruncLog) END Select 'Final Size of ' + db_name() + ' LOG is ' + CONVERT(VARCHAR(30),size) + ' 8K pages or ' + CONVERT(VARCHAR(30),(size*8/1024)) + 'MB' FROM sysfiles Where name = @LogicalFileName Drop TABLE DummyTrans SET NOCOUNT OFF
更改某個表
exec sp_changeobjectowner 'tablename','dbo'
存儲更改全部表
Create PROCEDURE dbo.User_ChangeObjectOwnerBatch @OldOwner as NVARCHAR(128), @NewOwner as NVARCHAR(128) AS DECLARE @Name as NVARCHAR(128) DECLARE @Owner as NVARCHAR(128) DECLARE @OwnerName as NVARCHAR(128) DECLARE curObject CURSOR FOR select 'Name' = name, 'Owner' = user_name(uid) from sysobjects where user_name(uid)=@OldOwner order by name OPEN curObject FETCH NEXT FROM curObject INTO @Name, @Owner WHILE(@@FETCH_STATUS=0) BEGIN if @Owner=@OldOwner begin set @OwnerName = @OldOwner + '.' + rtrim(@Name) exec sp_changeobjectowner @OwnerName, @NewOwner end -- select @name,@NewOwner,@OldOwner FETCH NEXT FROM curObject INTO @Name, @Owner END close curObject deallocate curObject GO
SQL SERVER中直接循環寫入數據
declare @i int set @i=1 while @i<30 begin insert into test (userid) values(@i) set @i=@i+1 end
創建資料庫
創建之前判斷該資料庫是否存在 if exists (select * from sysdatabases where name='databaseName') drop database 'databaseName' go Create DATABASE database-name
刪除資料庫
drop database dbname
備份sql server
--- 創建 備份數據的 device USE master EXEC sp_admpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 開始 備份 BACKUP DATABASE pubs TO testBack
創建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根據已有的表創建新表: A:create table tab_new like tab_old (使用舊表創建新表) B:create table tab_new as select col1,col2… from tab_old definition only
刪除新表
drop table tabname
增加一個列
Alter table tabname add column col type 註:列增加後將不能刪除。DB2中列加上後數據類型也不能改變,唯一能改變的是增加varchar類型的長度。
添加主鍵
Alter table tabname add primary key(col) 說明:刪除主鍵: Alter table tabname drop primary key(col)
創建索引
create [unique] index idxname on tabname(col….) 刪除索引:drop index idxname on tabname 註:索引是不可更改的,想更改必須刪除重新建。
創建視圖
create view viewname as select statement 刪除視圖:drop view viewname
幾個簡單的基本的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%』 (所有包含『value1』這個模式的字元串)---like的語法很精妙,查資料! 排序:select * from table1 order by field1,field2 [desc] 總數: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[separator]
幾個高級查詢運算詞
A: UNION 運算符 UNION 運算符通過組合其他兩個結果表(例如 TABLE1 和 TABLE2)並消去表中任何重復行而派生出一個結果表。當 ALL 隨 UNION 一起使用時(即 UNION ALL),不消除重復行。兩種情況下,派生表的每一行不是來自 TABLE1 就是來自 TABLE2。 B: EXCEPT 運算符 EXCEPT 運算符通過包括所有在 TABLE1 中但不在 TABLE2 中的行並消除所有重復行而派生出一個結果表。當 ALL 隨 EXCEPT 一起使用時 (EXCEPT ALL),不消除重復行。 C: INTERSECT 運算符 INTERSECT 運算符通過只包括 TABLE1 和 TABLE2 中都有的行並消除所有重復行而派生出一個結果表。當 ALL 隨 INTERSECT 一起使用時 (INTERSECT ALL),不消除重復行。 註:使用運算詞的幾個查詢結果行必須是一致的。
使用外連接
A、left outer join: 左外連接(左連接):結果集既包括連接表的匹配行,也包括左連接表的所有行。 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: 右外連接(右連接):結果集既包括連接表的匹配連接行,也包括右連接表的所有行。 C:full outer join: 全外連接:不僅包括符號連接表的匹配行,還包括兩個連接表中的所有記錄。
[編輯本段]提升
復製表
(只復制結構,源表名:a 新表名:b) (Access可用) 法一:select * into b from a where 1<>1 法二:select top 0 * into b from a
拷貝表
(拷貝數據,源表名:a 目標表名:b) (Access可用) insert into b(a, b, c) select d,e,f from b;
跨資料庫之間表的拷貝
(具體數據使用絕對路徑) (Access可用) insert into b(a, b, c) select d,e,f from b in 『具體資料庫』 where 條件 例子:..from b in '"&Server.MapPath("."&"\data.mdb" &"' where..
子查詢
(表名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)
顯示文章、提交人和最後回復時間
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
外連接查詢
(表名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
在線視圖查詢
(表名1:a select * from (Select a,b,c FROM a) T where t.a > 1;
between的用法
between限制查詢數據范圍時包括了邊界值,not between不包括 select * from table1 where time between time1 and time2 select a,b,c, from table1 where a not between 數值1 and 數值2
in 的使用方法
select * from table1 where a [not] in (『值1』,』值2』,』值4』,』值6』)
刪除主表中已經在副表中沒有的信息
兩張關聯表delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1
四表聯查問題
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 .....
日程安排提前五分鍾提醒
SQL: select * from 日程安排 where datediff('minute',f開始時間,getdate())>5
一條sql 語句搞定資料庫分頁
select top 10 b.* from (select top 20 主鍵欄位,排序欄位 from 表名 order by 排序欄位 desc) a,表名 b where b.主鍵欄位 = a.主鍵欄位 order by a.排序欄位
前10條記錄
select top 10 * form table1 where 范圍
選擇排名
選擇在每一組b值相同的數據中對應的a最大的記錄的所有信息(類似這樣的用法可以用於論壇每月排行榜,每月熱銷產品分析,按科目成績排名,等等.) select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
派生結果表
包括所有在 TableA 中但不在 TableB和TableC 中的行並消除所有重復行而派生出一個結果表 (select a from tableA except (select a from tableB) except (select a from tableC)
隨機取出10條數據
select top 10 * from tablename order by newid()
隨機選擇記錄
select newid()
刪除重復記錄
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
列出資料庫里所有的表名
select name from sysobjects where type='U'
列出表裡的所有的
select name from syscolumns where id=object_id('TableName')
列示排列
列示type、vender、pcs欄位,以type欄位排列,case可以方便地實現多重選擇,類似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 顯示結果: type vender pcs 電腦 A 1 電腦 A 1 光碟 B 2 光碟 A 2 手機 B 3 手機 C 3
初始化表table1
TRUNCATE TABLE table1
選擇從10到15的記錄
select top 5 * from (select top 15 * from table order by id asc) table_別名 order by id desc
數據類型轉換
declare @numid int declare @id varchar(50) set @numid=2005 set @id=convert(varchar,@numid) 通過上述語句完成數據類型Int轉換成varchar,其他轉換類似,可參看convert函數
[編輯本段]技巧
1=1,1=2的使用
在SQL語句組合時用的較多 「where 1=1」 是表示選擇全部 「where 1=2」全部不選, 如: if @strWhere !=' begin set @strSQL = 'select count(*) as Total from [' + @tblName + '] where ' + @strWhere end else begin set @strSQL = 'select count(*) as Total from [' + @tblName + ']' end 我們可以直接寫成 set @strSQL = 'select count(*) as Total from [' + @tblName + '] where 1=1 安定 '+ @strWhere
收縮資料庫
--重建索引 DBCC REINDEX DBCC INDEXDEFRAG --收縮數據和日誌 DBCC SHRINKDB DBCC SHRINKFILE
壓縮資料庫
dbcc shrinkdatabase(dbname) 轉移資料庫給新用戶以已存在用戶許可權 exec sp_change_users_login 'update_one','newname','oldname' go
檢查備份集
RESTORE VERIFYONLY from disk='E:\dvbbs.bak'
修復資料庫
Alter DATABASE [dvbbs] SET SINGLE_USER GO DBCC CHECKDB('dvbbs',repair_allow_data_loss) WITH TABLOCK GO Alter DATABASE [dvbbs] SET MULTI_USER GO

7. 怎樣用SQL語句查詢一個資料庫中的所有表

查詢一個資料庫中的所有表sql語句是show tables;

顯示所有資料庫的命令是:show databases;要查看某個資料庫先要進入資料庫使用user <資料庫名>命令;進入資料庫之後才能查詢資料庫中有哪些表。使用以下命令即可查出所有表:

show tables;

(7)用舊表查詢sql語句擴展閱讀

mysql資料庫的基本sql操作命令介紹:

1、顯示當前資料庫伺服器中的資料庫列表:mysql> SHOW DATABASES;

2、建立資料庫:mysql> CREATE DATABASE 庫名;

3、建立數據表:mysql> USE 庫名;mysql> CREATE TABLE 表名 (欄位名 VARCHAR(20), 字

名 CHAR(1));

4、刪除資料庫:mysql> DROP DATABASE 庫名;

5、刪除數據表:mysql> DROP TABLE 表名;

6、將表中記錄清空:mysql> DELETE FROM 表名;

7、往表中插入記錄:mysql> INSERT INTO 表名 VALUES ("hyq","M");

8、更新表中數據:mysql-> UPDATE 表名 SET 欄位名1='a',欄位名2='b' WHERE 欄位名3='c';

9、用文本方式將數據裝入數據表中:mysql> load data local infile "d:/mysql.txt" into table 表名;

10、導入.sql文件命令:mysql> USE 資料庫名;mysql> source d:/mysql.sql;

8. 從多個表中查詢數據的sql語句

用union,舉例有s1表(a,b,c,d)和s2表(a,c,d,e)和s3表(f,g),里頭的欄位不同,但在邏輯上有關系
(如有
s1.b=s2.e
s1.a=s3.f
s1.b=s3.g)
示例如下:
------------------------------------------------------------------------------
select
s1.a
as
x,s1.b
as
y,s1.c
as
z
from
s1
union
select
s2.a
as
x,s2.e
as
y,s2.c
as
z
from
s2
union
select
s3.f
as
x,s3.g
as
y,''
as
z
from
s3
------------------------------------------------------------------------------
最終結果會是三張表的和,如果s1有10條記錄,s2有3條記錄,s3有4條記錄,則執行本sql後會得到17條記錄,其中來自s3表的數據,第三列一定為空的。

9. sql查詢語句大全

SELECT * FROM TWS2F14CCC260D71 WHERE 地類='1999資源清查有林地'