]]>SQL CASE 语句http://www.aygfsteel.com/justfang/archive/2009/12/23/306984.htmljustjustWed, 23 Dec 2009 02:24:00 GMThttp://www.aygfsteel.com/justfang/archive/2009/12/23/306984.htmlhttp://www.aygfsteel.com/justfang/comments/306984.htmlhttp://www.aygfsteel.com/justfang/archive/2009/12/23/306984.html#Feedback0http://www.aygfsteel.com/justfang/comments/commentRss/306984.htmlhttp://www.aygfsteel.com/justfang/services/trackbacks/306984.htmlCaseh两种格式。简单Case函数和Case搜烦函数?nbsp; --单Case函数 CASE sex WHEN'1'THEN'?/span>' WHEN'2'THEN'?/span>' ELSE'其他'END --Case搜烦函数 CASEWHEN sex ='1'THEN'?/span>' WHEN sex ='2'THEN'?/span>' ELSE'其他'END
q两U方式,可以实现相同的功能。简单Case函数的写法相Ҏ较简z,但是和Case搜烦函数相比Q功能方面会有些限制Q比如写判断式?nbsp;
q有一个需要注意的问题QCase函数只返回第一个符合条件的|剩下的Case部分会被自动忽略?nbsp; --比如_下面q段SQLQ你永远无法得到“W二c?#8221;q个l果 CASEWHEN col_1 IN ( 'a', 'b') THEN'W一c?/span>' WHEN col_1 IN ('a') THEN'W二c?/span>' ELSE'其他'END
普通情况下Q用UNION也可以实现用一条语句进行查询。但是那样增加消?两个Select部分)Q而且SQL语句会比较长?br />
下面是一个是用Case函数来完成这个功能的例子 SELECT country, SUM( CASEWHEN sex ='1'THEN
population ELSE0END), --hh?/span> SUM( CASEWHEN sex ='2'THEN
population ELSE0END) --xh?/span> FROM Table_A GROUPBY country;
]]>SQL 外链接操作小l?inner join left join right joinhttp://www.aygfsteel.com/justfang/archive/2009/12/22/306936.htmljustjustTue, 22 Dec 2009 09:56:00 GMThttp://www.aygfsteel.com/justfang/archive/2009/12/22/306936.htmlhttp://www.aygfsteel.com/justfang/comments/306936.htmlhttp://www.aygfsteel.com/justfang/archive/2009/12/22/306936.html#Feedback0http://www.aygfsteel.com/justfang/comments/commentRss/306936.htmlhttp://www.aygfsteel.com/justfang/services/trackbacks/306936.html? 外部q接和自联接 inner join({D? 只返回两个表中联l字D늛{的?left join(左联? q回包括左表中的所有记录和双中联l字D늛{的记录 right join(双? q回包括双中的所有记录和左表中联l字D늛{的记录 on 指定表间联结字段及其关系的等?"=" 表达? q回 true ?false. 当表辑ּq回 true ? 则查询中包含该记? ! 外部q接只能操作已存在于数据库中的数?br />
update (ctarticle as a left join ctclass as c on a.classid = c.classid) left join cttag as b on a.articleid = b.articleid
set tag=tag+' ', b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid
where a.classid=23 and a.nclassid=0 and tagid is not null
update (ctarticle as a left join (ctnclass as c left join ctclass as d on c.classid = d.classid) on a.nclassid = c.nclassid and a.classid = c.classid) left join cttag as b on a.articleid = b.articleid set tag=d.class+' '+c.nclass, b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid where a.classid=23 and a.nclassid=197;
更新操作
左连接中数据的筛?nbsp;
insert into cttag(articleid,classid,nclassid) select a.articleid,a.classid,a.nclassid from ctarticle a left join cttag b on a.articleid=b.articleid where b.articleid is null
上例中的延箋
select a.*, b.*, c.*, d.*
from cttag as d left join ((ctarticle as a left join ctclass as b on a.classid=b.classid) left join ctnclass as c on a.nclassid=c.nclassid) on d.articleid=a.articleid;
昄文章表中的全? 调用cd表中的栏?nbsp;
select a.*, b.*, c.* from (ctarticle a left join ctclass b on a.classid=b.classid) left join ctnclass c on a.nclassid=c.nclassid
//作用, 有时在文章表中包含了在个别类别表中没有的数据, 用这个语法可以读出文章表的全部数?nbsp;
//a ?文章? b Zcd, c 为子cd
同上? 选择q加数据时加上空?nbsp;
insert into cttag(articleid,classid,nclassid,tag)
select a.articleid,a.classid,a.nclassid,d.class+' '+c.nclass
from (ctarticle as a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) left join cttag as b on a.articleid = b.articleid where a.classid=4 and a.nclassid=154;
q接n个表, q追加数据到其中一个表, n=4
insert into cttag(articleid,classid,nclassid,tag)
select a.articleid,a.classid,a.nclassid,d.class+c.nclass
from (ctarticle as a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) left join cttag as b on a.articleid = b.articleid where a.classid=1 and a.nclassid=1;
q接两个? q追加数据到其中一个表
insert into cttag(articleid,classid,nclassid)
select a.articleid,a.classid,a.nclassid
from ctarticle as a left join cttag as b on a.articleid = b.articleid where a.classid=1 and a.nclassid=1;
叛_q接
select a.*, b.* from bunclass a right join ctclass b on a.classid=b.classid where a.nclassid=20
查询别名 a,b ? 只匹?b 表中的内?
d数据到连接表之一
insert into cttag ( tag, articleid ) select top 1 b.tag, a.articleid from ctarticle as a left join cttag as b on a.articleid = b.articleid where a.articleid order by a.articleid desc;
变通中的用法二
insert into bureply
select b.*, a.classid, a.nclassid
from article as a inner join reply as b on a.articleid = b.articleid
where classid=50;
实际应用中的变?nbsp;
insert into butag ( tag, articleid, classid, nclassid)
select b.tag, a.articleid, a.classid, a.nclassid
from article as a inner join tag as b on a.articleid = b.articleid
where classid=24;
d数据到其他表
insert into butag ( tag, articleid )
select b.tag, a.articleid
from article as a inner join tag as b on a.articleid = b.articleid
where a.articleid<>false;
]]>SQL中IN,NOT IN,EXISTS,NOT EXISTS的用法和差别http://www.aygfsteel.com/justfang/archive/2009/12/22/306933.htmljustjustTue, 22 Dec 2009 09:26:00 GMThttp://www.aygfsteel.com/justfang/archive/2009/12/22/306933.htmlhttp://www.aygfsteel.com/justfang/comments/306933.htmlhttp://www.aygfsteel.com/justfang/archive/2009/12/22/306933.html#Feedback0http://www.aygfsteel.com/justfang/comments/commentRss/306933.htmlhttp://www.aygfsteel.com/justfang/services/trackbacks/306933.htmlSQL中IN,NOT IN,EXISTS,NOT EXISTS的用法和差别:
IN:定l定的值是否与子查询或列表中的值相匚w?br />
IN 关键字您得以选择与列表中的Q意一个值匹配的行?br />
当要获得居住?California、Indiana ?Maryland 州的所有作者的姓名和州的列表时Q就需要下列查询:
SELECT ProductID, ProductName FROM Northwind.dbo.Products WHERE CategoryID = 1 OR CategoryID = 4 OR CategoryID = 5
然而,如果使用 INQ少键入一些字W也可以得到同样的结果:
SELECT ProductID, ProductName FROM Northwind.dbo.Products WHERE CategoryID IN (1, 4, 5)
IN 关键字之后的目必须用逗号隔开Qƈ且括在括号中?br />
下列查询?titleauthor 表中查找在Q一U书中得到的版税于 50% 的所有作者的 au_idQ然后从 authors 表中选择 au_id ?br />
titleauthor 查询l果匚w的所有作者的姓名Q?nbsp;
SELECT au_lname, au_fname FROM authors WHERE au_id IN (SELECT au_id FROM titleauthor WHERE royaltyper < 50)
l果昄有一些作者属于少?50% 的一cR?br />
NOT IN:通过 NOT IN 关键字引入的子查询也q回一列零值或更多倹{?br />
以下查询查找没有出版q商业书c的出版商的名称?br />
SELECT pub_name FROM publishers WHERE pub_id NOT IN (SELECT pub_id FROM titles WHERE type = 'business')
使用 EXISTS ?NOT EXISTS 引入的子查询可用于两U集合原理的操作Q交集与差集。两个集合的交集包含同时属于两个原集合的所有元素?br />
差集包含只属于两个集合中的第一个集合的元素?br />
EXISTS:指定一个子查询Q检行的存在?nbsp;
本示例所C查询查扄位于以字?B 开头的城市中的M出版商出版的书名Q?br />
SELECT DISTINCT pub_name FROM publishers WHERE EXISTS (SELECT * FROM titles WHERE pub_id = publishers.pub_id AND type =
'business')
SELECT distinct pub_name FROM publishers WHERE pub_id IN (SELECT pub_id FROM titles WHERE type = 'business')
两者的区别:
EXISTS:后面可以是整句的查询语句?SELECT * FROM titles
IN:后面只能是对单列:SELECT pub_id FROM titles
NOT EXISTS:
例如Q要查找不出版商业书c的出版商的名称Q?br />
SELECT pub_name FROM publishers WHERE NOT EXISTS (SELECT * FROM titles WHERE pub_id = publishers.pub_id AND type =
'business')
下面的查询查扑ַl不销售的书的名称:
SELECT title FROM titles WHERE NOT EXISTS (SELECT title_id FROM sales WHERE title_id = titles.title_id)