MYSQL - GROUP BY clause at Professional Level

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahulephp
    New Member
    • Sep 2009
    • 59

    #1

    MYSQL - GROUP BY clause at Professional Level

    How to fetch the "name" FROM programname = "Buy" and description which has maximum words using GROUP BY??

    I have 6 columns with thousands of product entries:
    Table name : eproducts
    Columns : product_id, programname, name, description, clean_modelno

    Ex. Products Entries:

    product_id : 5001
    programname : argos
    name : Toshiba 32RV753B
    description : By Toshiba (100 words)
    model_number : 32RV753B
    price : 549.99

    product_id : 5002
    programname : Buy
    name : Toshiba 32 Inch Full HD 1080p Freeview HD LCD TV
    description : Television picture quality: Freeview HD digital (Approx 110 words)
    model_number : 32RV753B
    price : 499

    product_id : 5003
    programname : Amazon
    name : Toshiba 32 Inch Widescreen Full HD 1080p LCD TV
    desciption : 66cm Visible LCD HD Ready Integrated Digital (Approx 90 words)
    model_number : 32RV753B
    price : 650

    product_id : 5004
    programname : Ebey
    name : 2RV753B 32Inch HD Ready 1080p LC HD Read
    description : Toshiba 32RV753 / 32RV753B 32" REGZA RV Serie (Approx 97 words)
    model_number : 32RV753B
    price : 550

    When I Group by the products using there model numbers, it gave me the first row as output as all these four products has same model number:

    Code:
    SELECT *,  
    								
    MAX(price) as max_price,
    
    MIN(price) as min_price,
    
    count(distinct programname) as total_retailers
    
    FROM eproducts as e
    
    WHERE 
    GROUP BY model_number

    BUT,
    I want to get the "name" FROM programname : Buy and description which has maximum words from all four descriptions.

    Please suggest me a good and efficient solution.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    I'm not clear on what you are trying to achieve. Could you explain better what kind of output you want? An example of the desired output would also be helpful.

    Comment

    • rahulephp
      New Member
      • Sep 2009
      • 59

      #3
      Ohh. I am sorry if you couldn't understand.
      Let me explain,

      Description from argos is always good as compare to buy.
      In the same way Argos > buy > amazon > ebay > Next
      I want to select Description from argos if present else from buy else from amazon.

      It mean,
      Code:
      Code:
      if(programname = Argos )
      {
         description = description; //Argos description
      }
      elseif(programname = buy)
      {
        description = description;  //buy's description
      }
      elseif(programname = amazon )
      {
        description = description;  //amazon's description
      }
      elseif(programname = ebay )
      {
        description = description;  //ebay's description
      }
      else
      {
      description = description;  //Default Next's description
      }
      GROUP BY model_number
      The GROUP BY clause always give me the description from first entry which comes first in the queue whatever it will be.

      Please help me out, Will be highly thankful to you.

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        What about
        Code:
        WHERE description = description
        I don't understand what you are trying to do with GROUP BY

        Comment

        Working...