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

sql編寫

發布時間: 2022-01-12 09:34:21

sql語句編寫

多次update欄位?沒看懂你要問啥。

至於表A中的一條記錄對應表B多條記錄(取其中兩個欄位相乘再累加賦值給表A),我寫個例子,你參考一下,不難的

updateaseta.zian=b.zian
fromtable1a
join(selectc.guanlianzian,sum(c.zian1*c.zian2)aszianfromtable2c)bwherea.guanlianzian=b.guanlianzian

㈡ sql語句編寫

select t1.*, t2.level, t3.effectivity
from 表1 t1 left join 表2 t2 on t1.groupID=t2.groupID
left join 表t3 on t1.groupID=t3.groupID
不知道表3中的groupID與表1中的groupID的關系,所以只用了left join,也可能表3需要過濾出唯一對應的groupID,但我不知道,這只能你自己進行修改了

㈢ sql語句編寫。

給你舉個例子吧
SELECT [id]

,[userZT]

FROM [CF_webSever].[dbo].[Table_CF_userData]

(//條件)

whereid = 『20』

意思是查詢這個表[Table_CF_userData] 中的 ID = 20的數據 得出userZT 值 如果你userZT 改為 * 號,就是返回所有欄位的值。


update[CF_webSever].[dbo].[Table_CF_userData] SET

(//更新的欄位值)

[userZT] = '過期',[username]='張三'

(//條件)

whereid = '20'

意思是更新ID=20的數據中的 userZT username 這2個欄位的值

deleteFROM [CF_webSever].[dbo].[Table_CF_userData]

(//條件)

where id = 『20』

意思是刪除 id = 20 的數據

INSERT INTO [CF_webSever].[dbo].[Table_CF_userData]

(//插入條件)

([userZT] ,[username] )

VALUES

('生效','李四')

意思是插入到這個表 [Table_CF_userData] 的數據值,

注意格式一定要寫對。


純手工打的。如果有問題再問。

㈣ 如何用sql語句編寫

select case when substr(bwm,6,6) = dwbm then 'Same'
else 'different' end as result

from tablename;

㈤ 編寫SQL語句

DECLARE @str varchar(1000)
declare @price float
declare @count int
DECLARE @idoc int;
DECLARE @doc xml;

declare cur cursor for select ID,價格 from 表1
open cur
fetch next from cur into @str,@price
while @@FETCH_STATUS<>-1
begin
set @doc=cast('<Root><item><ID>'+replace(@str,',','</ID></item><item><ID>')+'</ID></item></Root>' as xml)
EXEC sp_xml_preparedocument @Idoc OUTPUT, @doc
SELECT @count=COUNT(*) FROM OPENXML (@Idoc, '/Root/item',2)
WITH (
[ID] varchar(10)
)
update t1 set t1.價格=@price/@count from 表2 t1 inner join (SELECT ID FROM OPENXML (@Idoc, '/Root/item',2)
WITH (
[ID] varchar(10)
) ) t2 on t1.ID=t2.id
fetch next from cur into @str,@price
end
close cur
deallocate cur
總體思路:對表1各行ID進行拆分,然後分別更新表2的數據

㈥ SQL語言編寫怎麼編

一、
1,select * from 學生 where 學生號 in (select 學生號 from 選課)

2, select * from 學生 where 學生號 in (select 學生號 from 選課 having count(課程號)=3)
3, select 選課.學生號,學生.姓名,課程.課程名 from 選課 ,學生,課程
where 選課.學生號=學生.學生號 and 選課.課程號=課程.課程號 group by 選課.學生號 ,選課.成績 ;
4,select a1.課程號,a1.課程名,count(*) as 選課人數 from 課程 a1,選課 a2 where a1.課程號=a2.課程號 group by a1.課程號,a1.課程名
5,select * from 課程 where 課程號 in( select 課程號 from 選課 having count(學生號)>=5);

二、
1、select b2.商品代號,b1.單價,b1.數量,b2.產地 from 商品表1 b1,商品表2 b2 where b1.商品代號 = b2.商品代號
2、select distinct 產地 from 商品表2
3、select top 1 * from 商品表1 order by 數量 desc

㈦ sql代碼編寫

沒有表結構,我這里假設有2個表
一個部門表, 一個員工表(包含一個部門 ID)

SELECT * FROM 部門表
WHERE 部門ID IN
(
SELECT
TOP 1
部門ID
FROM
員工表
GROUP BY
部門ID
ORDER BY
COUNT(1) DESC, 部門ID
)

㈧ sql語句的編寫

你把union改為union all 應該就行了。
如果不行就加一個隱含欄位,編為1,2,3,然後再按照他排序。

㈨ 請教如何用SQL語句編寫

(1) select Cname, count(SC.Sno) as cnt from C inner join SC on C.Cno=SC.Cno group by SC.Cno
(2) select Sname count(SC.Cno) as cnt from S inner join SC on S.Sno=SC.Sno group by SC.Sno having count(SC.Cno)>=3
(3) select S.Sname, SC.Grade from S inner join SC on S.Sno=SC.Sno where SC.Cno=1 and SC.Grade>=90

㈩ sql如何寫

創建之前判斷該資料庫是否存在
if exists (select * from sysdatabases where name='databaseName')
drop database databaseName
go
Create DATABASE databasename
on primary-- 默認就屬於primary文件組,可省略

/*--數據文件的具體描述--*/
name=『databasename_data』,-- 主數據文件的邏輯名稱
filename=『'所存位置:\databasename_data.mdf』, -- 主數據文件的物理名稱
size=數值mb, --主數據文件的初始大小
maxsize=數值mb, -- 主數據文件增長的最大值
filegrowth=數值%--主數據文件的增長率

log on

/*--日誌文件的具體描述,各參數含義同上--*/
name='databasename_log', -- 日誌文件的邏輯名稱
filename='所存目錄:\databasename_log.ldf', -- 日誌文件的物理名稱
size=數值mb, --日誌文件的初始大小
filegrowth=數值mb--日誌文件的增長值

刪除資料庫
drop database databasename
備份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] identity(起始值,遞增量)
,col2 type2 [not null],..)--primary key為主鍵 identity表示遞增數量
根據已有的表創建新表:
A:go
use 原資料庫名
go
select * into 目的資料庫名.dbo.目的表名 from 原表名(使用舊表創建新表)
B:create table tab_new as select col1,col2… from tab_old definition only
創建序列
create sequence SIMON_SEQUENCE
minvalue 1 -- 最小值
maxvalue 999999999999999999999999999 -- 最大值
start with 1 -- 開始值
increment by 1 -- 每次加幾
cache 20;
刪除表
drop table tabname--這是將表連同表中信息一起刪除但是日誌文件中會有記錄
刪除表中信息
delete from tabname-這是將表中信息刪除但是會保留這個表
增加一個列
Alter table tabname add colname coltype
刪除一個列
Alter table tabname drop column colname
添加主鍵
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語句
(1) 數據記錄篩選:
sql="select * from 數據表 where欄位名=欄位值 order by欄位名[desc]"
sql="select * from 數據表 where欄位名like '%欄位值%' order by 欄位名 [desc]"
sql="select top 10 * from 數據表 where欄位名=欄位值 order by 欄位名 [desc]"
sql="select top 10 * from 數據表 order by 欄位名 [desc]"
sql="select * from 數據表 where欄位名in ('值1','值2','值3')"
sql="select * from 數據表 where欄位名between 值1 and 值2"
(2) 更新數據記錄:
sql="update 數據表 set欄位名=欄位值 where 條件表達式"
sql="update 數據表 set 欄位1=值1,欄位2=值2 …… 欄位n=值n where 條件表達式"
(3) 刪除數據記錄:
sql="delete from 數據表 where 條件表達式"
sql="delete from 數據表" (將數據表所有記錄刪除)
(4) 添加數據記錄:
sql="insert into 數據表 (欄位1,欄位2,欄位3 …) values (值1,值2,值3 …)"
sql="insert into 目標數據表 select * from 源數據表" (把源數據表的記錄添加到目標數據表)
(5) 數據記錄統計函數:
AVG(欄位名) 得出一個表格欄平均值
COUNT(*;欄位名) 對數據行數的統計或對某一欄有值的數據行數統計
MAX(欄位名) 取得一個表格欄最大的值
MIN(欄位名) 取得一個表格欄最小的值
SUM(欄位名) 把數據欄的值相加
引用以上函數的方法:
sql="select sum(欄位名) as 別名 from 數據表 where 條件表達式"
set rs=conn.excute(sql)
用 rs("別名") 獲取統計的值,其它函數運用同上。
查詢去除重復值:select distinct * from table1
(5) 數據表的建立和刪除:
CREATE TABLE 數據表名稱(欄位1 類型1(長度),欄位2 類型2(長度) …… )
(6) 單列求和:
SELECT SUM(欄位名) FROM 數據表
編輯本段
最新語句

查詢資料庫中含有同一這欄位的表:
select name from sysobjects where xtype = 'u' and id in(select id from syscolumns where name = 's3')
根據出生日期可以算出年齡:
select datediff(year,scrq,'2013') as 年齡 from page_shsjgrgl
根據當前年份自動算出年齡
select datediff(year,csny,cast(YEAR(GETDATE()) as char))

select year(djsj) from page_shsjgrgl

select month(djsj) from page_shsjgrgl

select day(djsj) from page_shsjgrgl
在同一資料庫中復製表結構:
select * into a from b where 1<>1
當 IDENTITY_INSERT 設置為 OFF 時,不能為表 'aa' 中的標識列插入顯式值。
set identity_insert aa ON----設置打開,
批量插入:
insert into aa(Customer_ID, ID_Type, ID_Number) select Customer_ID, ID_Type, ID_Number from TCustomer;
set identity_insert aa OFF---關閉
不同資料庫之間的復制:
復制結構:
select * into test.dbo.b from GCRT.dbo.page_shsjgrgl where 1<>1
復制內容:
insert into test.dbo.b(xm,ssdq) select xm,ssdq from GCRT.dbo.page_shsjgrgl
查看資料庫中所有的數據表表名:
select name from SysObjects where type='u'
查看資料庫中所有表含有同一欄位的表:
select name from sysobjects where xtype = 'u' and id in(select id from syscolumns where name = '同一欄位')
查看數據表中的所有欄位:
select name from Syscolumns where id=object_id('表名')
查詢資料庫時隨機10條記錄:
select top 10 * from td_areacode order by newid()
修改欄位類型:
ALTER TABLE 表名 ALTER COLUMN 欄位名 varchar(30) NOT NULL
use ZHJIANGJGYL
declare @temp nvarchar(30)
set @temp = 'ZWI4'
select hllx from page_yljg_zyry where hllx not in(
select
case @temp when ''
then ''
else b1 end
from (
select * from TD_Code where page_en='page_yljg_zyry' and B2='ZWI'
) s where s.b1 !=
case @temp when '' then '' else @temp end
)
更改資料庫表欄位類型:
alter table page_shsjgrgl alter column s1 int
高級查詢
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:
全外連接:不僅包括符號連接表的匹配行,還包括兩個連接表中的所有記錄。
編輯本段
判斷對象

判斷資料庫是否存在
if exists (select * from sysdatabases where name= '資料庫名')
dropdatabase[資料庫名]
判斷表是否存在
if not exists (select * from sysobjects where [name] = '表名' and xtype='U')
begin
--這里創建表
end
判斷存儲過程是否存在
if exists (select * from sysobjects whereid = object_id(N'[存儲過程名]') and OBJECTPROPERTY(id, N'IsProcere') = 1)
dropprocere[存儲過程名]
判斷臨時表是否存在
if object_id('tempdb..#臨時表名') isnot null
droptable#臨時表名
判斷視圖是否存在
--SQL Server 2000
IF EXISTS (SELECT * FROM sysviews WHEREobject_id = '[dbo].[視圖名]'
--SQL Server 2005
IF EXISTS (SELECT * FROM sys.views WHERE object_id = '[dbo].[視圖名]'
判斷函數是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函數名]') and xtype in (N'FN', N'IF', N'TF'))
dropfunction[dbo].[函數名]
獲取創建信息
SELECT[name],[id],crdate FROM sysobjects where xtype='U'
/*
xtype 的表示參數類型,通常包括如下這些 C = CHECK約束 D = 默認值或DEFAULT約束 F = FOREIGNKEY約束 L = 日誌 FN = 標量函數 IF = 內嵌表函數 P = 存儲過程 PK = PRIMARYKEY約束(類型是K) RF = 復制篩選存儲過程 S = 系統表 TF = 表函數 TR = 觸發器U = 用戶表 UQ = UNIQUE約束(類型是K) V = 視圖 X = 擴展存儲過程 */
判斷列是否存在
if exists(select * from syscolumns whereid=object_id('表名') and name='列名')
altertable表名dropcolumn列名
判斷列是否自增列
if columnproperty(object_id('table'),'col','IsIdentity')=1
print '自增列'
else
print '不是自增列'
SELECT * FROM sys.columns WHEREobject_id=OBJECT_ID('表名')
AND is_identity=1
判斷表中是否存在索引
if exists(select * from sysindexes whereid=object_id('表名') and name='索引名')
print '存在'
else
print '不存在
查看資料庫中對象
SELECT * FROM sysobjects WHEREname='對象名'
select * from table(所要查詢的表名) where coloum(條件)
編輯本段
提升

復製表
(只復制結構,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1<>1
法二:select top 0 * into b from a
拷貝表
(拷貝數據,源表名:a 目標表名:b) (Access可用)
insert into b(x, y, z) select d,e,f from a;
跨資料庫之間表的拷貝
(具體數據使用絕對路徑) (Access可用)
insert into b(x, y, z) select d,e,f from a 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 * from 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 top10 * from (select top 15 * from table order by id asc) table_別名 order by id desc) table_2 order by id
數據類型轉換
declare @numid int
declare @id varchar(50)
set @numid=2005
set @id=convert(varchar,@numid)
通過上述語句完成數據類型Int轉換成varchar,其他轉換類似,可參看convert函數