mapid firstID secondID thirdID forthID n
----------- ----------- ----------- ----------- ----------- -----------
1 1 2 3 4 2
2 2 3 4 5 1
3 3 4 5 2 3
4 4 3 2 1 4
有如上一个表,firstID secondID thirdID forthID 存的都是userid,现在根据传入的userid查询mapid,n=1就查firstID=userid的行,n=2就查secondID=userid的行
希望是存储过程……
新手求指教
----------- ----------- ----------- ----------- ----------- -----------
1 1 2 3 4 2
2 2 3 4 5 1
3 3 4 5 2 3
4 4 3 2 1 4
有如上一个表,firstID secondID thirdID forthID 存的都是userid,现在根据传入的userid查询mapid,n=1就查firstID=userid的行,n=2就查secondID=userid的行
希望是存储过程……
新手求指教
解决方案 »
- sql leftjoin多表查询,出现重复的数据,怎么处理?
- mssql2005 开发版、企业版、express版有啥差别
- 日期格式调整
- 关于LINQToSQL中出现的排序+去重复出现的问题
- ms sql view(视图)为何有两条重复记录
- id IN('1','6','8','10','2')如何按照顺序输出,就是in中的顺序,请高手指点,谢谢,急急
- 请问这个SQL哪儿错了?(在线等)
- 一个让我巨头痛的问题,效率太低了,各位老大指点一下
- 如何取Datetime類型 如 2003-03-01 00:00:00.000 的年月 如 2003-03 ?尋最簡方法
- 数据库设计高手请进!讨论数据库设计问题。(有好思路另开主题给分)
- 关于区域更改后日期的问题
- 急~~求高手!!!sql 如何合并記錄,如下
from tb
where (n=1 and firstid = ?)
or (n=2 and secondid = ?)
or (n=3 and thirdid = ?)
or (n=4 and forthid = ?)
from tab
where n=1 and firstID = @userid -- @userid是参数
union all
select *
from tab
where n=2 and secondID = @userid -- @userid是参数
union all
select *
from tab
where n=3 and thirdID = @userid -- @userid是参数
union all
select *
from tab
where n=4 and forthID = @userid -- @userid是参数
现在根据传入的userid查询mapid,n=1就查firstID=userid的行,n=2就查secondID=userid的行
--你这句话,根据userid怎么知道n等于几呢?还是所有像小三说的那意思?
where (case n when 1 then firstID when 2 then secondId
when 3 then thirdID else forthID end)=@userid