Professional Web Applications Themes

simple counting and selection - MySQL

I am having trouble figuring out the proper syntax for the following: a. order a table by a numeric field attribute. b. select the bottom or top x number of rows. for instance- Suppose I have the table 'Planet': planetname varchar(20), number_of_moons varchar(2) How would I select all of the planets with less than 2 moons?...

  1. #1

    Default simple counting and selection


    I am having trouble figuring out the proper syntax for the following:

    a. order a table by a numeric field attribute.
    b. select the bottom or top x number of rows.

    for instance- Suppose I have the table 'Planet': planetname
    varchar(20), number_of_moons varchar(2)

    How would I select all of the planets with less than 2 moons?

    samuraishowdown@gmail.com Guest

  2. #2

    Default Re: simple counting and selection

    i believe i could simply say:


    SELECT planetname FROM Planet WHERE number_of_moons < 2;

    easy enough I suppose, BUT what about if I wanted to order the table
    according
    to the number_of_moons field and then select the top 5? So I could get
    the 5 names of
    the 5 planets with the most moons.

    samuraishowdown@gmail.com Guest

  3. #3

    Default Re: simple counting and selection

    [email]samuraishowdown[/email] wrote:
    > i believe i could simply say:
    >
    >
    > SELECT planetname FROM Planet WHERE number_of_moons < 2;
    >
    > easy enough I suppose, BUT what about if I wanted to order the table
    > according
    > to the number_of_moons field and then select the top 5? So I could get
    > the 5 names of
    > the 5 planets with the most moons.

    May I suggest finding a tutorial on the web, or purchasing a book on SQL.

    SELECT planetname,number_of_moons FROM Planet WHERE number_of_moons > 0
    ORDER BY number_of_moons DESC LIMIT 0,5;


    --
    Brian Wakem
    Email: [url]http://homepage.ntlworld.com/b.wakem/myemail.png[/url]
    Brian Wakem Guest

  4. #4

    Default Re: simple counting and selection

    Brian Wakem wrote:
    > [email]samuraishowdown[/email] wrote:
    >
    >
    >>i believe i could simply say:
    >>
    >>
    >>SELECT planetname FROM Planet WHERE number_of_moons < 2;
    >>
    >>easy enough I suppose, BUT what about if I wanted to order the table
    >>according
    >>to the number_of_moons field and then select the top 5? So I could get
    >>the 5 names of
    >>the 5 planets with the most moons.
    >
    >
    >
    > May I suggest finding a tutorial on the web, or purchasing a book on SQL.
    >
    > SELECT planetname,number_of_moons FROM Planet WHERE number_of_moons > 0
    > ORDER BY number_of_moons DESC LIMIT 0,5;
    >
    >
    Actually this sounds like a homework question to me...

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    [email]jstucklexattglobal.net[/email]
    ==================
    Jerry Stuckle Guest

Similar Threads

  1. counting down in a for
    By drowl@23.me.uk in forum PERL Beginners
    Replies: 4
    Last Post: December 12th, 04:14 PM
  2. Counting (easy!) (YES!!)
    By Trent Rigsbee in forum PERL Beginners
    Replies: 2
    Last Post: November 13th, 02:35 AM
  3. [PHP] Simple Selection Box
    By Scott Miller in forum PHP Development
    Replies: 0
    Last Post: September 8th, 08:01 PM
  4. Simple hilighting/text selection question
    By Jay in forum Macromedia Director Lingo
    Replies: 1
    Last Post: August 15th, 07:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139