论坛首页 综合技术论坛

考考你的SQL水平

浏览 57311 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (13) :: 隐藏帖 (1)
作者 正文
   发表时间:2012-05-02  
select a.name from (
select name from test where hobby='basketball' )a join
(select name from test where hobby='badminton' ) b
on a.name=b.name
0 请登录后投票
   发表时间:2012-05-03  
select * from test where name in(select name from test where HOBBY='badminton') and HOBBY = 'basketball'
union all
select * from test where name in(select name from test where HOBBY='basketball') and HOBBY = 'badminton';
0 请登录后投票
   发表时间:2012-05-03  
select* from test where name in(
select name from test where hobby in ('basketball', 'badminton')
  group by name having count(name)=2)
0 请登录后投票
   发表时间:2012-05-04  
SELECT * FROM `TEST` T WHERE T.NAME IN
(SELECT T1.NAME FROM `TEST` T1 WHERE T1.HOBBY=`basketball`)
AND T.HOBBY='badminton'
0 请登录后投票
   发表时间:2012-05-04  
select test.* from
(
select  distinct Name from test
where HOBBY = 'basketball' or  HOBBY = 'badminton'
group by Name
Having COUNT(HOBBY) =2
) as a inner join test
on a.NAME = test.NAME
where HOBBY = 'basketball' or  HOBBY = 'badminton'
0 请登录后投票
   发表时间:2012-05-04   最后修改:2012-05-04


SELECT *
  FROM (SELECT NAME, COUNT(T.HOBBY) AS HNUM
          FROM TEST T
         WHERE UPPER(T.HOBBY) = 'BADMINTON'
            OR UPPER(T.HOBBY) = 'BASKETBALL'
         GROUP BY T.NAME
         ORDER BY T.NAME) TEMP
 WHERE TEMP.HNUM > 1
 

 

 

0 请登录后投票
   发表时间:2012-05-04   最后修改:2012-05-04
SELECT *
  FROM TEST
 WHERE NAME IN (SELECT NAME
                  FROM TEST T
                 WHERE (UPPER(T.HOBBY) = 'BADMINTON' OR
                       UPPER(T.HOBBY) = 'BASKETBALL') HAVING
                 COUNT(T.HOBBY) > 1
                 GROUP BY T.NAME)

 ORDER BY NAME DESC
 
0 请登录后投票
   发表时间:2012-05-04  
SELECT *
  FROM TEST TEST
 WHERE EXISTS (SELECT 1
          FROM TEST T
         WHERE (UPPER(T.HOBBY) = 'BADMINTON' OR
               UPPER(T.HOBBY) = 'BASKETBALL')
           AND T.NAME = TEST.NAME HAVING COUNT(T.HOBBY) > 1
         GROUP BY T.NAME)
 ORDER BY NAME ASC
 
0 请登录后投票
   发表时间:2012-05-04   最后修改:2012-05-04
既然已经知道basketball和badminton了,就只需要列出Name就可以了,然后在程序中对应做name - hobby。

select name from test where hobby in ('basketball','badminton') group by name having count(hobby)=2;
0 请登录后投票
   发表时间:2012-05-04  
select a.NAME from tt a where a.HOBBY in('basketball','badminton')
group by a.NAME having count(a.NAME) = 2
0 请登录后投票
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics