SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    finding max value col from a single row in MySql

    Hi,

    Suppose i've a single row in my table :

    id recordid sug1 sug2 sug3 sug4 sug5 sug6
    1 1 3 2 4 2 1 6

    Now, I want to find out the maximum value col in that row i mean sug6 corresponding to recordid 1 , i am struggling to write a query for this, can someone help me in this?


    Thanks,
    kul.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT id
         , recordid
         , GREATEST(sug1,sug2,sug3,sug4,sug5,sug6) AS daGreatest 
      FROM daTable
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply,

    i tried this, but it returns the greatest value, not the name of the col containing greatest value, wheeras i need to find out the name of that col.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    my advice is to normalize your data

    any time you have six apparently identical columns like that, chances are good that the table is not in first normal form

    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
  •