跪求高手解答

lihongdian 2009-06-26 05:33:49
我现在有一个这样的表
LineID SeqNo RegID
---------------------------
743 1 101
743 99 102
744 1 101
744 2 404
744 99 501

其中lineID表示线路ID.比如743这条线路表示的是从1->99的路线,启始101,到102.
744这条线路表示的是从1->99的路线,启始101,到404,到501

请问有没有办法用一条sql语句取出,启始101,到102的这条线路呢?高手求教!!!!!

...全文
58 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
thinklys 2009-06-27
  • 打赏
  • 举报
回复
学习中
feixianxxx 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 feixianxxx 的回复:]
SQL code-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================

IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb(lineID int,seqNO int ,RegID int)
go
insert into tb
select 743,1,101 union all
select 743,99,102 union all
select 744,1,101 union all
select 744,2,404 un…
[/Quote]
测试下~
feixianxxx 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 lihongdian 的回复:]
引用 9 楼 fredrickhu 的回复:
SQL codeselect distinct LineID from tb t
where exists(select 1 from tb where LineID=t.LineID and SeqNo=1 and RegID=101)
and exists(select 1 from tb where LineID=t.LineID and SeqNo=99 and RegID=102)


这个是我想要的结果,有没有比这更快的吗?
[/Quote]
看下 十楼的。。
ai_li7758521 2009-06-26
  • 打赏
  • 举报
回复
select LineID from tb t
where exists(select 1 from tb where LineID=t.LineID and SeqNo=1 and RegID=101)
and exists(select 1 from tb where LineID=t.LineID and SeqNo=99 and RegID=102)
group by LineID
baetgc 2009-06-26
  • 打赏
  • 举报
回复
学习
lihongdian 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fredrickhu 的回复:]
SQL codeselect distinct LineID from tb t
where exists(select 1 from tb where LineID=t.LineID and SeqNo=1 and RegID=101)
and exists(select 1 from tb where LineID=t.LineID and SeqNo=99 and RegID=102)
[/Quote]
这个是我想要的结果,有没有比这更快的吗?
ljking0731 2009-06-26
  • 打赏
  • 举报
回复
drop table #temp

create table #temp(LineID int, Seqno int, RegID int)

insert into #temp
select 743, 1, 101 union all
select 743, 99, 102 union all
select 744, 1, 101 union all
select 744, 2, 404 union all
select 744, 99, 501

LineID Seqno RegID
----------- ----------- -----------
743 1 101
743 99 102
744 1 101
744 2 404
744 99 501

(5 行受影响)

---

select LineID from
(
select LineID,RegID from #temp
where RegID in (101,102)) as a
group by LineID having count(lineID)>1

LineID
-----------
743

(1 行受影响)
SQL77 2009-06-26
  • 打赏
  • 举报
回复
[code=SQL]create table [tb]([LineID] int,[SeqNo] int,[RegID] int)
insert [tb]
select 743,1,101 union all
select 743,99,102 union all
select 744,1,101 union all
select 744,2,404 union all
select 744,99,501


select
distinct LineID
from tb t
where exists(select 1 from tb where LineID=t.LineID and RegID=101 and exists(select 1 from tb where LineID=t.LineID and RegID=102))

SELECT LineID FROM TB WHERE RegID IN (101 ,102 ) GROUP BY LINEID HAVING COUNT(LINEID)>=2

--DROP TABLE TB
[/code]
ai_li7758521 2009-06-26
  • 打赏
  • 举报
回复
declare @t table ([LineID] int,[SeqNo] int,[RegID] int)
insert @t
select 743,1,101 union all
select 743,99,102 union all
select 744,1,101 union all
select 744,2,404 union all
select 744,99,501

select [LineID]
from
(
select [LineID],B_RegID=min([RegID]),E_RegID=max([RegID])
from @t
group by [LineID]
) A
where B_RegID=101 and E_RegID=102

LineID
-----------
743
(1 行受影响)
izbox 2009-06-26
  • 打赏
  • 举报
回复

if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([LineID] int,[SeqNo] int,[RegID] int)
Insert #T
select 743,1,101 union all
select 743,99,102 union all
select 744,1,101 union all
select 744,2,404 union all
select 744,99,501


SELECT LineID from #T
where RegId=101 AND SeqNo=1 and LineID in(
SELECT LineID from #T
where RegId=102 AND SeqNo=99)
feixianxxx 2009-06-26
  • 打赏
  • 举报
回复
-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================

IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb(lineID int,seqNO int ,RegID int)
go
insert into tb
select 743,1,101 union all
select 743,99,102 union all
select 744,1,101 union all
select 744,2,404 union all
select 744,99,501
go
select LINEID
from tb

group by LINEID
having min(RegID)=101 and MAX(RegID)<=102--因为REGID是INT 类型 ,所以 限定 最小为101 最大为 102 路线就确定了
/*------------
743
-------*/
feixianxxx 2009-06-26
  • 打赏
  • 举报
回复
-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================

IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb(lineID int,seqNO int ,RegID int)
go
insert into tb
select 743,1,101 union all
select 743,99,102 union all
select 744,1,101 union all
select 744,2,404 union all
select 744,99,501
go
select LINEID
from tb

group by LINEID
having min(RegID)=101 and MAX(RegID)<=102
/*------------
743
-------*/
--小F-- 2009-06-26
  • 打赏
  • 举报
回复
select distinct LineID from tb t
where exists(select 1 from tb where LineID=t.LineID and SeqNo=1 and RegID=101)
and exists(select 1 from tb where LineID=t.LineID and SeqNo=99 and RegID=102)
SQL77 2009-06-26
  • 打赏
  • 举报
回复
create table [tb]([LineID] int,[SeqNo] int,[RegID] int)
insert [tb]
select 743,1,101 union all
select 743,99,102 union all
select 744,1,101 union all
select 744,2,404 union all
select 744,99,501

SELECT LineID FROM TB WHERE RegID IN (101 ,102 ) GROUP BY LINEID HAVING COUNT(LINEID)>=2

LineID
-----------
743

(所影响的行数为 1 行)
SQL77 2009-06-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lihongdian 的回复:]
SeqNo RegID
---------------------------
743 1 101
743 99 102
744 1 101
744 2 404
744 99 501

我意思很简单,请你取出743这个ID
条件是你只知道: 当RegID=101的时候SeqNo=1,当RegID=102的时候SeqNo=99
[/Quote]
SELECT LineID FROM TB WHERE RegID IN (101 ,102 ) GROUP BY LINEID HAVING COUNT(LINEID)>=2
feixianxxx 2009-06-26
  • 打赏
  • 举报
回复
-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================

IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb(lineID int,seqNO int ,RegID int)
go
insert into tb
select 743,1,101 union all
select 743,99,102 union all
select 744,1,101 union all
select 744,2,404 union all
select 744,99,501
go
alter function poof(@n int)
returns varchar(100)
as
begin
declare @s varchar(100)
set @s=''
select @s=@s+'-'+convert(varchar(100),SEQNO)
from tb
where lineID =@n
return stuff(@s,1,1,'')
end
go
select LINEID,dbo.poof(LINEID)as 路线
from tb
group by LINEID
having MAX(RegID)<=102
/*------------
743 1-99
-------*/
等不到来世 2009-06-26
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([LineID] int,[SeqNo] int,[RegID] int)
insert [tb]
select 743,1,101 union all
select 743,99,102 union all
select 744,1,101 union all
select 744,2,404 union all
select 744,99,501
go
--select * from [tb]

select distinct LineID
from tb t
where exists(select 1 from tb where LineID=t.LineID and SeqNo=1 and RegID=101)
and exists(select 1 from tb where LineID=t.LineID and SeqNo=99 and RegID=102)
/*
LineID
-----------
743

(1 行受影响)
*/
jiangshun 2009-06-26
  • 打赏
  • 举报
回复
select * from 表 where RegID=101 and SeqNo=1 union all
select * from 表 where RegID=102 and SeqNo=99
lihongdian 2009-06-26
  • 打赏
  • 举报
回复
就是要同时满足这2个条件时结果743才正确
lihongdian 2009-06-26
  • 打赏
  • 举报
回复
LineID SeqNo RegID
---------------------------
743 1 101
743 99 102
744 1 101
744 2 404
744 99 501

我意思很简单,请你取出743这个ID
条件是你只知道: 当RegID=101的时候SeqNo=1,当RegID=102的时候SeqNo=99
加载更多回复(1)
博乐—机器人表演 机器人演出的开拓者 www.boole-tech.com 1 DIY 机器人 第一课 制作机器人的基础 在论坛上经常可以看到有人说:"我是菜鸟,制作机器人需要什么基础?"。其 实我可以回答大家:"基础就是你首先要学好数理化",怎么样很意外吧!现在 我来给大家讲讲为什么这么说。机器人制作综合性很强,它涉及机械制造、计 算机、电子技术等很多领域,每一个领域都是以我们的数理化为基础的,还有就 是要充分利用互联网的资源自我学习。 以下是我在论坛发过的一篇贴子,希望大家用心体会。 高手的秘诀 1.不要看到别人的回复第一句话就说:给个代码吧!你应该想想为什么。当你自己想 出来 再参考别人的提示,你就知道自己和别人思路的差异。 2.别小家子气,买本书几十块都舍不得,你还学个 P。为了省钱看电子书,浪费的时间绝对 超过书的价值。当然如果查资料,只能看 PDF。 3.学习新的开发软件时,一定要看帮助手册。买的书不够全面。刚接触一个软件,什么都不 懂,就盲目的问东问西,让人看起来很幼稚。 4.不要蜻蜓点水,得过且过,细微之处往往体现实力。 5.把时髦的技术挂在嘴边,还不如把过时的技术记在心里。 6.看得懂的书,请仔细看;看不懂的书,请硬着头皮看。 博乐—机器人表演 机器人演出的开拓者 www.boole-tech.com 2 .别指望看第一遍书就能记住和掌握什么——请看第二遍、第三遍。 7.多实践,去焊板子、调试,去写去调,只用软件模拟,是永远成不了高手的。 8.保存好你做过的所有的源程序、PCB、原理图等----那是你最好的积累之一。 9.对于网络,还是希望大家能多利用一下,很多问题不是非要到论坛来问的, 首先你要学会 自己找答案,比如 google、百度都是很好的搜索引擎,你只要输入关键字就 能找到很多相 关资料,别老是等待别人给你希望,看的出你平时一定也很懒! 10 到一个论坛,你学会去看以前的帖子,不要什么都不看就发帖子问,也许你的问题早就 有人问过了,你再问,别人已经不想再重复了, 做为初学者,谁也不希望自己的帖子没人回 的。 11,虽然不是打击初学者,但是这句话还是要说:论坛论坛,就是大家讨论的地方,如果 你 总期望有高手总无偿指点你,除非他是你亲戚!! 讨论者, 起码是水平相当的才有讨论的说 法,如果水平真差距太远了,连基本操作都需要别人给解答,谁还跟你讨论呢。 什么样的人是浮躁的人? 浮躁的人容易问:我到底该学什么; ----踏踏实实的学点基本的吧?单片机不知道是什么就想去学 ARM? c 语言不会想搞 LINUX?别老是好高骛远。 浮躁的人容易问:谁有 xxx 源码?--(你给人家多少钱啊?自己的劳动白送你?) 浮躁的人容易说:跪求 xxx ---(就算网络也要点尊严吧?) 浮躁的人容易说:紧急求救---(其实只是个简单的课程设计) 浮躁的人容易说:有没有 xxx 中文资料?---一个字:懒。别说别的。E 文不行?谁不是 博乐—机器人表演 机器人演出的开拓者 www.boole-tech.com 3 从 ABC 学起的啊? 浮躁的人容易说:求 xxx,我的 email 是 xxx@xxx.com,然后消失 ---- 你以为你是大爷啊, 人 家请你吃饭,还要喂到你口里不成? 浮躁的人容易问:做单片机有钱途吗----只是为了钱,搞不好技术的,你去抢银行好了。 浮躁的人容易说:哪里有 xxx 芯片资料?(其实大部分资料网络上都有,但是偏偏来找人 问,懒!) 浮躁的人分两种:只观望而不学的人;只学而不坚持的人; 浮躁的人永远不是一个高手。 lamp 看完后明白了吧,恭喜!你已经迈出了第一步。 第一课就到这里,下课接着讲。 _________________ 第二课 机器人的组成 机器人一般是由以下模块组成: 1.语音模块 用于语音对话、语音控制。 2.灯光表达模块 用于机器人状态显示、感情表达(喜、怒、哀、乐) 3.控制模块 I/O+决策,即机器人的大脑,其控制能力及 AI 由编程者的水平决定。 博乐—机器人表演 机器人演出的开拓者 www.boole-tech.com 4 4.传感器模块 对外界环境的的感知。 5.电源模块 为机器人提供能源,常用有 3v、5v、6v、9v、12v。 6.减速电机 常用于机器人行走驱动,根据需要选择变比。 7.驱动模块 机器人的执行机构,常选用行模中的舵机。 8.视频模块 机器人的眼 9.机械模块 10.无线通讯模块 11.控制软件 一个简单的机器人是由单片机电路、红外传感器、减速电机+程序这是大多数人 认同的,但是我要向大家说这种认识是错误的,真正的机器人不是几片单机和 几台电脑就可以解决,关键就是'机器人'中这个'人'字,还需要我们一代代 的努力。 以上讲的有点简单,先写出。 --

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧