变态的SQL查询,人快疯了

yql1986 2010-09-14 04:43:29

table1
-------|------|------|------|------|------|------|------|--------------
solt0 |solt1 |solt2 |solt4 |solt5 |solt6 |solt7 |solt8 |solt9
-------|------|------|------|------|------|------|------|--------------
101 111 121 311 413 2341 1141 123 116
-------|------|------|------|------|------|------|------|--------------
501 101 567 897 145 190 782 124 212
-------|------|------|------|------|------|------|------|--------------
1123 2341 1245 216 316 210 219 34561 8431
-------|------|------|------|------|------|------|------|--------------


table 2 [id]是主键

------|------|--
id name
------|------|--
101 xxx1
------|------|--
1110 xxx2
------|------|--
121 xxx3
------|------|--
3110 xxx4
------|------|--
4113 xxx5
------|------|--
21341 xxx6
------|------|--
1141 xxx7
------|------|--
123 xxx8
------|------|--
116 xxx9
------|------|--

...省略 table2中id是table1中id的集合 .
问题是将table1中的solt与table2中的id相结合 找出相同项

现在想要的查询结果


------|------|--
id name
------|------|--
101 xxx1
------|------|--
111 xxxx
------|------|--
121 xxx3
------|------|--
311 xxxx
------|------|--
413 xxx5
------|------|--
21341 xxx6
------|------|--
1141 xxx7
------|------|--
123 xxx8
------|------|--
116 xxx9
------|------|--
501 xxxx
------|------|--
567 xxxx
------|------|--
897 xxx
------|------|--
145 xxxx
------|------|--
190 xxxxx
------|------|--
782 xxxx
------|------|--
... 省略
...全文
128 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yql1986 2010-09-14
  • 打赏
  • 举报
回复
wufeng4552 太强悍了
天下如山 2010-09-14
  • 打赏
  • 举报
回复
学习 回复很强大!
昵称被占用了 2010-09-14
  • 打赏
  • 举报
回复
select *
from table2
where id in
(select solt0 solt from table1
union all
select solt1 from table1
union all
select solt2 from table1
union all
select solt3 from table1
union all
select solt4 from table1
union all
select solt5 from table1
union all
select solt6 from table1
union all
select solt7 from table1
union all
select solt8 from table1
union all
select solt9 from table1)
昵称被占用了 2010-09-14
  • 打赏
  • 举报
回复
用in好点,怕重复

yhtapmys 2010-09-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hao1hao2hao3 的回复:]
引用 1 楼 wufeng4552 的回复:
SQL code
select b.*
from table2 b,
(select solt0 solt from table1
union all
select solt1 from table1
union all
select solt2 from table1
union all
select solt3 from tab……
[/Quote]

up
claro 2010-09-14
  • 打赏
  • 举报
回复
MySQL?

我已经疯了,给数据折腾的。
hao1hao2hao3 2010-09-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wufeng4552 的回复:]
SQL code
select b.*
from table2 b,
(select solt0 solt from table1
union all
select solt1 from table1
union all
select solt2 from table1
union all
select solt3 from table1
union all
select s……
[/Quote]


太牛了。
也可以把table1和table2关联10次,每次关联table1中不同的字段。

不过没有上面的方法简单。
水族杰纶 2010-09-14
  • 打赏
  • 举报
回复
select b.*
from table2 b,
(select solt0 solt from table1
union all
select solt1 from table1
union all
select solt2 from table1
union all
select solt3 from table1
union all
select solt4 from table1
union all
select solt5 from table1
union all
select solt6 from table1
union all
select solt7 from table1
union all
select solt8 from table1
union all
select solt9 from table1)a
where b.id=a.solt

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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