SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict ankitaneja1's Avatar
    Join Date
    Sep 2004
    Location
    India
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    selecting randomly from database:plz help

    i want to select any 20 rows randomly from database
    i am using sql server 2000
    and asp.net
    each time user open the page i want that 20 items randomly selected from database which should be different each time should be displayed
    plz help
    Ankit Aneja
    "Nothing is impossible. The word itself says - I M possible"

  2. #2
    SitePoint Zealot CatsEyeDesigns's Avatar
    Join Date
    Dec 2003
    Location
    Florida
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not a SQL expert and I don't really know anything about .Net, but your SQL statement should have an "order by" on the end of it and "rand()" *should* (though I'm not certain) be supported by MSSQL.

    For example:
    select * from tableName order by rand()


    Hope that helps.

  3. #3
    SitePoint Addict ankitaneja1's Avatar
    Join Date
    Sep 2004
    Location
    India
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am having list of products say 1000
    i want to randomly show 20 of them which should be different each time i log in or open the window
    can any type of timer can be set
    say i am having coloumn "prod_name" and "lid" in row
    how can i randomly generate 20 names from those 1000 names
    Ankit Aneja
    "Nothing is impossible. The word itself says - I M possible"

  4. #4
    SitePoint Member
    Join Date
    Nov 2004
    Location
    INDIA
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In My opnion Mr Ankit Aneja, You look to BE great programmer ,but i am just entered the field, i just can give you logic.
    get any 20 Randomly generated number and according to them do the Query to generate the Result.

    hope you like my idea,if not i am very sorry
    Cheers,
    Alok Gupta
    visit me at http://www.thatsalok.com

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    select top 20
           prod_name
         , lid
      from yourtable
    order by newid()
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •