这个简单sql语句如何写 ( 积分: 100 )

  • 主题发起人 主题发起人 wzgss
  • 开始时间 开始时间
W

wzgss

Unregistered / Unconfirmed
GUEST, unregistred user!
id&nbsp;userName&nbsp;book<br>&nbsp;0&nbsp;&nbsp;&nbsp;张三&nbsp;&nbsp;&nbsp;&nbsp;书1<br>&nbsp;1&nbsp;&nbsp;&nbsp;张三&nbsp;&nbsp;&nbsp;&nbsp;书2<br>&nbsp;2&nbsp;&nbsp;&nbsp;李四&nbsp;&nbsp;&nbsp;&nbsp;书2<br>&nbsp;3&nbsp;&nbsp;&nbsp;李四&nbsp;&nbsp;&nbsp;&nbsp;书3<br>&nbsp;....<br>表的意思是人,人拥有的书。要查询同时拥有某些书的用户的SQL怎么写呢?<br><br>同时拥有某一部书的用户查询:<br>&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;username&nbsp;from&nbsp;(select&nbsp;username&nbsp;from&nbsp;table&nbsp;where&nbsp;book='书1')&nbsp;tmpTable&nbsp;group&nbsp;by&nbsp;username。<br>但如果查询一部以上的书这么写就不行了:select&nbsp;username&nbsp;from&nbsp;(select&nbsp;username&nbsp;from&nbsp;table&nbsp;where&nbsp;book='书1'&nbsp;or&nbsp;book='书2')&nbsp;tmpTable&nbsp;group&nbsp;by&nbsp;username。&nbsp;查询结果有可能不是同时拥有书1和书2了,如果用having,但having后面跟聚合函数,总通不过。
 
不明白你为什么搞这么复杂,可能我理解的不对吧。<br><br>查一部书时<br>select&nbsp;username&nbsp;from&nbsp;table&nbsp;where&nbsp;book='书1'<br>这样不就行了?难道你的表里面有可能一个人拥有两本相同的书?<br><br>查多部书时<br>select&nbsp;username&nbsp;from&nbsp;table&nbsp;where&nbsp;book&nbsp;in&nbsp;('书1',&nbsp;'书2'……)&nbsp;group&nbsp;by&nbsp;username<br>这样不能解决吗?
 
不太明白你的意思<br>select&nbsp;username&nbsp;from&nbsp;(select&nbsp;username&nbsp;from&nbsp;table&nbsp;where&nbsp;book='书1'&nbsp;or&nbsp;book='书2')&nbsp;tmpTable&nbsp;group&nbsp;by&nbsp;username&nbsp;huving&nbsp;count(username)&gt;1,是这意思吗
 
楼主写的SQL是什么意思!搞得这么复杂!<br>select&nbsp;username&nbsp;from&nbsp;table&nbsp;where&nbsp;book='书1'不就OK了?
 
select&nbsp;book,UserName&nbsp;from&nbsp;Table1&nbsp;where&nbsp;book&nbsp;in&nbsp;(select&nbsp;distinct&nbsp;book&nbsp;from&nbsp;Table1)
 
同时拥有书1和书2的人只有张三,没有李四<br>你看你们的查询李四肯定包含!
 
select&nbsp;book,UserName&nbsp;from&nbsp;Table1&nbsp;as&nbsp;a&nbsp;where&nbsp;exists(select&nbsp;1&nbsp;from&nbsp;table1&nbsp;as&nbsp;b<br>where&nbsp;a.book=b.book&nbsp;&nbsp;and&nbsp;a.username&lt;&gt;b.username)<br>两个人姓名一样的话会不准确。<br>给分吧!
 
不明白你为什么搞这么复杂,可能我理解的不对吧。<br><br>查一部书时<br>select&nbsp;username&nbsp;from&nbsp;table&nbsp;where&nbsp;book='书1'<br>这样不就行了?难道你的表里面有可能一个人拥有两本相同的书?<br><br>查多部书时<br>select&nbsp;username&nbsp;from&nbsp;table&nbsp;where&nbsp;book&nbsp;in&nbsp;('书1',&nbsp;'书2'……)&nbsp;group&nbsp;by&nbsp;username<br>这样不能解决吗?&nbsp;&nbsp;<br>================================================================================<br>支持~~~~&nbsp;<br>加一句&nbsp;就算“一个人拥有两本相同的书”&nbsp;又有什么关系呢?<br>select&nbsp;username&nbsp;from&nbsp;table&nbsp;where&nbsp;book='书1'&nbsp;group&nbsp;by&nbsp;username,book<br>不就行了吗?
 
select&nbsp;*<br>from<br> (select&nbsp;userName,<br> sum(case&nbsp;book&nbsp;when&nbsp;'书1'&nbsp;then&nbsp;1&nbsp;else&nbsp;0&nbsp;end)&nbsp;书1,<br> sum(case&nbsp;book&nbsp;when&nbsp;'书2'&nbsp;then&nbsp;1&nbsp;else&nbsp;0&nbsp;end)&nbsp;书2<br> from&nbsp;<br> (select&nbsp;*<br> from&nbsp;tb<br> where&nbsp;book='书1'<br> or&nbsp;book='书2')&nbsp;a<br> group&nbsp;by&nbsp;UserName)&nbsp;b<br>where&nbsp;书1&gt;0<br>and&nbsp;书2&gt;0
 
select&nbsp;username&nbsp;<br>from&nbsp;<br>&nbsp;&nbsp;(select&nbsp;username,book&nbsp;<br>&nbsp;&nbsp;from&nbsp;Table&nbsp;<br>&nbsp;&nbsp;where&nbsp;book='书1'&nbsp;or&nbsp;book='书2'&nbsp;group&nbsp;by&nbsp;username,&nbsp;book)&nbsp;tmpTable&nbsp;<br>group&nbsp;by&nbsp;username&nbsp;<br>having&nbsp;count(username)&gt;1
 
我认为最好的答案是我头上的
 
select&nbsp;username&nbsp;from&nbsp;表<br>where&nbsp;book&nbsp;in&nbsp;('书2','书3')<br>group&nbsp;by&nbsp;username<br>having&nbsp;count(*)&nbsp;=&nbsp;2&nbsp;&nbsp;//查同时拥有n本书就count(*)&nbsp;=&nbsp;n
 
最好的答案都在后面了,朋友如果要分支声一下
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
615
import
I
I
回复
0
查看
850
import
I
后退
顶部