每个用户在 从表(t) 只获取一条 最新的 记录。适用于一对多,但每个主表记录只查询一条从表记录的情况。
addtime -- 添加时间
userid -- 外键
select top 10 title from t where addtime in (select max(addtime) from t group by userid)
参考网址:
http://zhidao.baidu.com/link?url=-H1FS4dw5QTSpQQnaLTBvpIh1lgag4e6jabw2bIk7ZpTYM7n3PYl93Dhes4w3HVoOIbAvAIOcVu5RMIrBwmcwq
// 2014-08-20 update
// 获取某个分类下固定条数的记录,支持排序
// 文章表 art_art, 分类字段clsid
SQL in 排序
select id, job_id, username, true_name as name, course, face_image as face, sort_id
from [TABLE_NAME]
where job_id in (1,10,11,12,13,14,2394) order by charindex(',' + rtrim(cast(job_id as varchar(10))) + ',', ',1,10,11,12,13,14,2394,'), sort_id asc;
数据按照in内部的ID排序 而不是默认的混乱的排序。
最后的 sort_id asc 会进行二次排序,结果如下:(job_id安装括号内顺序,sort_id按照 asc 升序)
// 2016-04-03
// 在SQL Select 语句中使用 case when then 语句 自定义输出内容
// 2016-05-10
// SQL 中的 intersect 用法
在一个多对多的关系表中, 假设有 产品表 PRD, 属性表ATTR, 关系表 RELS
RELS表中结构 PID | AID
现在需要读取AID 既包含 = 1 又包含 = 2 的产品ID, 单纯的IN 是一种或OR的关系, 这里需要且AND的关系, 所以先读取=1的然后再读取=2的, 再取交集即可
语句如下:
select id, name from PRD where id in ( select pid from rels where aid = 1 intersect select pid from rels where aid = 2 )
addtime -- 添加时间
userid -- 外键
select top 10 title from t where addtime in (select max(addtime) from t group by userid)
参考网址:
http://zhidao.baidu.com/link?url=-H1FS4dw5QTSpQQnaLTBvpIh1lgag4e6jabw2bIk7ZpTYM7n3PYl93Dhes4w3HVoOIbAvAIOcVu5RMIrBwmcwq
// 2014-08-20 update
// 获取某个分类下固定条数的记录,支持排序
// 文章表 art_art, 分类字段clsid
select * from
(
select id, title, clsid, num = row_number() over(partition by clsid order by addtime desc) from art_art
where clsid in (98, 102, 20)
) as tmp
where num <= 5
// 2015-02-09
SQL 全文索引相关 contains(), freetext() 语法
https://msdn.microsoft.com/en-us/library/ms187787.aspx
// 2015-05-20
// 2015-02-09
SQL 全文索引相关 contains(), freetext() 语法
https://msdn.microsoft.com/en-us/library/ms187787.aspx
SQL in 排序
select id, job_id, username, true_name as name, course, face_image as face, sort_id
from [TABLE_NAME]
where job_id in (1,10,11,12,13,14,2394) order by charindex(',' + rtrim(cast(job_id as varchar(10))) + ',', ',1,10,11,12,13,14,2394,'), sort_id asc;
数据按照in内部的ID排序 而不是默认的混乱的排序。
最后的 sort_id asc 会进行二次排序,结果如下:(job_id安装括号内顺序,sort_id按照 asc 升序)

// 2016-04-03
// 在SQL Select 语句中使用 case when then 语句 自定义输出内容
select
title, username, danwei, classname, puttime,
(
case [source]
when 'shzh' then '装潢网'
when 'snzsxh' then '协会'
end
) as [source_name],
(
case [status]
when 10 then '处理中'
when 20 then '已处完成'
end
) as [status_name]
from [tablename]
where puttime > '2015-04-01' and recycle = 0
order by puttime desc
// 2016-05-10
// SQL 中的 intersect 用法
在一个多对多的关系表中, 假设有 产品表 PRD, 属性表ATTR, 关系表 RELS
RELS表中结构 PID | AID
现在需要读取AID 既包含 = 1 又包含 = 2 的产品ID, 单纯的IN 是一种或OR的关系, 这里需要且AND的关系, 所以先读取=1的然后再读取=2的, 再取交集即可
语句如下:
select id, name from PRD where id in ( select pid from rels where aid = 1 intersect select pid from rels where aid = 2 )
关于SQL Server将一列的多行内容拼接成一行的问题讨论 (转)
(原地址:http://www.cnblogs.com/rainman/archive/2013/05/03/3058413.html)
昨天遇到一个SQL Server的问题:需要写一个储存过程来处理几个表中的数据,最后问题出在我想将一个表的一个列的多行内容拼接成一行,比如表中有两列数据 :
类别 | 名称 |
---|---|
AAA | 企业1 |
AAA | 企业2 |
AAA | 企业3 |
BBB | 企业4 |
BBB | 企业5 |
我想把这个表变成如下格式:
类别 | 名称 |
---|---|
AAA | 企业1,企业2,企业3 |
BBB | 企业4,企业5 |
一开始挺头疼的(会了的肯定没有这种感觉,不会那必须是头疼啊(*^__^*) ),从网上找了点资料,算是找到一种比较简单方便的方法吧,现在大体总结一下,供大家共同学习。
原先的表名为Table_A,实现代码如下:
select 类别, 名称 = ( stuff( (select ',' + 名称 from Table_A where 类别 = A.类别 for xml path('')), 1, 1, '' ) ) from Table_A as A group by 类别