Ⅰ sql語句的select用法
因為typeld是字元類型的,如果是數值型的就回這么寫'+typeld+'
舉個列子如果從A 表查詢到typeld的值是aaa,當轉變為可執行的語句的時候,sum這段會變成
sum(case when typeld='aaa' then usermoney else 0 end)
如果按數值型的寫法'+typeld+'
就會變成sum(case when typeld=aaa then usermoney else 0 end)
這里aaa是字元型數據,語法就回出錯了。
至於單引號和雙引號的問題,是這樣的:
兩個單引號內,如果需要表示字元的單引號,單引號就要寫成兩個單引號。
給你幾個語句執行看看結果就知道了
select '''' 這里是四個單引號 ,結果一個單引號
select '"' 這里是兩單引號內含一個雙引號 ,結果雙引號
select ''' 這里是三個單引號 ,結果 報錯了
Ⅱ SQL SELECT 語句
--學生表,欄位:ID,名字,年齡
declare
@student
table(
stu_id
int,
stu_name
varchar(16)
age
int
)
--課程表,欄位:ID,名字,學分
declare
@course
table(
cou_id
int,
cou_name
varchar(12)
cou_score
int
)
--學生選修課程表,欄位:學生ID,課程ID,分數
declare
@sc
table(
stu_id
int,
cou_id
int,
score
int
)
--插入數據
insert
@student
select
1,'劉德華',47
union
all
select
1,'張學友',46
(1)select
count(distinct
cou_name)
as
'選修課程門數'
from
@course
(2)select
avg(sum(age))
as
'選修C4課程的學生的平均年齡'
from
@student
where
stu_id
in
(select
distinct
stu_id
from
@sc
where
cou_id
in
(select
distinct
cou_id
from
@course
where
cou_name='c4'))
(3)select
avg(sum(score))
as
'學分為3的每門課程的學生平均成績'
from
@sc
where
cou_id
in
(select
distinct
cou_id
from
@course
where
cou_score=3)
(4)select
stu_name
as
'學號比王菲大,年齡比他小的學生姓名'
from
@student
where
stu_id>(select
distinct
stu_id
from
@studnet
where
stu_nme='王菲')
and
age<(select
distinct
age
from
@student
where
stu_name='王菲')
Ⅲ SQL資料庫語句大全
SQL資料庫語句大全:
1、選擇:select
*
from
table1
where
范圍
2、插入:insert
into
table1(field1,field2)
values(value1,value2)
3、刪除:delete
from
table1
where
范圍
4、更新:update
table1
set
field1=value1
where
范圍
5、排序:select
*
from
table1
order
by
field1,field2
[desc]
6、總數:select
count
as
totalcount
from
table1
7、求和:select
sum(field1)
as
sumvalue
from
table1
幾個高級查詢運算詞:
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),不消除重復行。
Ⅳ sql的select語句
where stcd = 'hy_stsc_a.dbo.stcd'
上面這個條件,你確定么?
單引號之間的內容,這么寫就是字元串了。
也就是說stcd這個欄位里,有那個'hy_stsc_a.dbo.stcd' 串?
還有,你的p列,子查詢並沒有和外邊的表hy_stsc_a相連,這樣查詢即使不空,所有行的p欄位肯定也是一樣的。
Ⅳ sql 語句大全
1、說明:創建資料庫
CREATE DATABASE database-name
2、說明:刪除資料庫
drop database dbname
3、說明:備份sql server
--- 創建 備份數據的 device
USE master
EXEC sp_admpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- 開始 備份
BACKUP DATABASE pubs TO testBack
4、說明:創建新表
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
5、說明:刪除新表
drop table tabname
6、說明:增加一個列
Alter table tabname add column col type
註:列增加後將不能刪除。DB2中列加上後數據類型也不能改變,唯一能改變的是增加varchar類型的長度。
7、說明:添加主鍵: Alter table tabname add primary key(col)
說明:刪除主鍵: Alter table tabname drop primary key(col)
8、說明:創建索引:create [unique] index idxname on tabname(col….)
刪除索引:drop index idxname
註:索引是不可更改的,想更改必須刪除重新建。
9、說明:創建視圖:create view viewname as select statement
刪除視圖:drop view viewname
10、說明:幾個簡單的基本的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%』 ---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
11、說明:幾個高級查詢運算詞
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),不消除重復行。
註:使用運算詞的幾個查詢結果行必須是一致的。
12、說明:使用外連接
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/cross (outer) join:
全外連接:不僅包括符號連接表的匹配行,還包括兩個連接表中的所有記錄。
12、分組:Group by:
一張表,一旦分組 完成後,查詢後只能得到組相關的信息。
組相關的信息:(統計信息) count,sum,max,min,avg 分組的標准)
在SQLServer中分組時:不能以text,ntext,image類型的欄位作為分組依據
在selecte統計函數中的欄位,不能和普通的欄位放在一起;
13、對資料庫進行操作:
分離資料庫: sp_detach_db;附加資料庫:sp_attach_db 後接表明,附加需要完整的路徑名
14.如何修改資料庫的名稱:
sp_renamedb 'old_name', 'new_name'
Ⅵ sql select 語句
把我在您另一個提問中的答案跟這個整合一下就行了,語句如下:
SELECT ext FROM ext_ok_dialplan WHERE ext NOT IN (SELECT ext FROM ext_no_vdn) and exists(select 1 from range where ext_start<=ext_ok_dialplan.ext and ext_end>=ext_ok_dialplan.ext)
Ⅶ sql中的select 語句
SELECT Owner_id,COUNT(Owner_id) AS HYGS into #ls FROM BEFRIEND WHERE
Friend_id IN (select Friend_id from BEFRIEND where Owner_id='A') --與A共同好友關系的人
AND Friend_id!='A' --與A非好友關系
order by HYGS desc
select top 10 * from SITEUSER where Id in(select * from #ls)
Ⅷ SQL中有哪些select語句查詢數據
幾個簡單的基本的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 * from table1 group by field1 ORDER BY count(ShopId) LIMIT 20 (兼並排序分頁) 總數: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] 查詢去除重復值:select distinct * from table1 使用外連接 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: 全外連接:不僅包括符號連接表的匹配行,還包括兩個連接表中的所有記錄。
Ⅸ SQL select語句
1.select
count
course
from
sc
group
by
course
2.select
avg
age
from
sc,s
where
sc
sno=s
sno
and
course='c4'
3.
select
avg
grade
from
sc
where
學分='3'
4.select
sname
from
s
where
sno>(select
sno
from
s
where
sname='王非')
and
sage
<(select
sage
from
s
where
sname='王非')
如果表名和欄位名對的話,就直接復制到查詢分析器里就成了