美文网首页
153、MySQL入门(三):ORDER BY 语句用法

153、MySQL入门(三):ORDER BY 语句用法

作者: 陈容喜 | 来源:发表于2018-04-25 16:50 被阅读0次

    Sqlzoo习题练习:SELECT from nobel

    习题链接:<u>http://sqlzoo.net/wiki/SELECT_from_Nobel_Tutorial</u>

    下面为SELECT from nobel习题内容:

    --#1
    /*
    Change the query shown so that it displays Nobel prizes for 1950.
    */
    SELECT yr, subject, winner
    FROM nobel
    WHERE yr = 1950
    
    --#2
    /*
    Show who won the 1962 prize for Literature.
    */
    SELECT winner
      FROM nobel
     WHERE yr = 1962
       AND subject = 'Literature'
    
    --#3
    /*
    Show the year and subject that won 'Albert Einstein' his prize.
    */
    SELECT yr,subject
    FROM nobel
    WHERE winner = 'Albert Einstein'
    
    --#4
    /*
    Give the name of the 'Peace' winners since the year 2000, including 2000.
    */
    SELECT winner FROM nobel
    WHERE (subject LIKE 'Peace%') AND (yr >= 2000)
    
    --#5
    /*
    Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive
    */
    SELECT *
    FROM nobel
    WHERE (yr BETWEEN 1980 AND 1989) AND (subject = 'Literature')
    
    --#6
    /*
    Show all details of the presidential winners:
    Theodore Roosevelt
    Woodrow Wilson
    Jimmy Carter
    */
    SELECT * FROM nobel
     WHERE winner IN ('Theodore Roosevelt',
                      'Woodrow Wilson',
                      'Jimmy Carter',
                      'Barack Obama')
    
    --#7
    /*
    Show the winners with first name John
    */
    SELECT winner FROM nobel
    WHERE winner LIKE 'John%'
    
    --#8
    /*
    Show the Physics winners for 1980 together with the Chemistry winners for 1984.
    */
    SELECT *
    FROM nobel
    WHERE (subject = 'PhysicS' AND yr = 1980) OR (subject = 'Chemistry' AND yr = 1984)
    
    --#9
    /*
    Show the winners for 1980 excluding the Chemistry and Medicine
    */
    SELECT * FROM nobel
    WHERE YR = 1980 AND subject NOT IN ('Chemistry','Medicine')
    
    --#10
    /*
    Show who won a 'Medicine' prize in an early year (before 1910, not including 1910) 
    together with winners of a 'Literature' prize in a later year (after 2004, including 2004)
    */
    SELECT * FROM nobel
    WHERE (subject = 'Medicine' AND yr < 1910) OR (subject = 'Literature' AND yr >= 2004) 
    
    --#11
    /*
    当查询的内容中出现特殊字符时,使LIKE 语句进行查询。
    例如:下面的名字中Ü为特殊字符,使用LIKE 语句中的%代替。
    Find all details of the prize won by PETER GRÜNBERG
    */
    SELECT * FROM nobel
    WHERE winner LIKE 'peter gr%nberg'
    
    --#12
    /*
    Find all details of the prize won by EUGENE O'NEILL
    不能直接引用人名中的一个引号。可以在引用的字符串中使用两个单引号。
    */
    SELECT * FROM nobel
    WHERE winner = 'EUGENE O''NEILL'
    
    --#13
    /*
    List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.
    在上面的问题中需要使用ORDER BY 语句对结果进行排序
    ORDER BY 语句用于根据指定的列对结果集进行排序。
    ORDER BY 语句默认按照升序对记录进行排序。
    如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。
    针对上面的问题先对结果中的年份进行降序排序(先显示最新的),
    如果年份相同再按名字字母顺序排序。
    具体操作如下:
    */
    SELECT winner,yr,subject
    FROM nobel
    WHERE winner LIKE 'sir%'
    ORDER BY yr DESC,winner
    
    --#14
    /*
    The expression subject IN ('Chemistry','Physics') can be used as a value - it will be 0 or 1.
    Show the 1984 winners ordered by subject and winner name; but list Chemistry and Physics last.
    */
    SELECT winner, subject,subject IN ('Physics','Chemistry')
    FROM nobel
    WHERE yr=1984
    ORDER BY subject IN ('Physics','Chemistry'),subject,winner
    
    

    相关文章

      网友评论

          本文标题:153、MySQL入门(三):ORDER BY 语句用法

          本文链接:https://www.haomeiwen.com/subject/viyylftx.html