php distinct problem

Discussion in 'PHP' started by Greenmethod, Jul 30, 2007.

  1. #1
    Basically what I want to do is select the "Chain_store" AND "Shop" from this database so that I can display the Shop.

    $Chains = mysql_db_query("*******","SELECT DISTINCT Chain_store FROM customer WHERE Chain_store != ' '");
    PHP:

     
    Greenmethod, Jul 30, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try <> instead of !=

    != is a php construct not an sql one.

    ("*******","SELECT DISTINCT Chain_store FROM customer WHERE Chain_store <> ' '");

    Although, you probably need to test for null and empty string rather than just a space?

    ("*******","SELECT DISTINCT Chain_store FROM customer WHERE Chain_store <> ' ' AND Chain_store <> '' AND Chain_store is not NULL");


    Other than that, your query is fine.
     
    ecentricNick, Jul 31, 2007 IP
  3. HuggyCT2

    HuggyCT2 Guest

    Messages:
    222
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can use != that is a logical operator in MySQL
     
    HuggyCT2, Jul 31, 2007 IP
  4. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok, my bad!

    but, in re-reading the question, I think i may have misunderstood anyway. I think perhaps the poster wants distinct chain stores and distinct shops to be returned? But, I'm not really clear. So, Greenmethod, can you expand on your question a little?

    If you have seperate columns called Chainstore and Shop and on any row just one of those columns is filled in (which breaks 3rd normal form but never mind) then you'd do something akin....

    SELECT DISTINCT Chain_store FROM customer WHERE Chain_store != ' '
    UNION
    SELECT DISTINCT Shop FROM customer WHERE Shop != ' '

    But I still think you need to check for nulls and empty strings unless you are explicitly stuffing spaces into those columns when you insert the records.
     
    ecentricNick, Jul 31, 2007 IP
  5. nagasharmi

    nagasharmi Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    select distinct(fieldname) from table name where <condition>
     
    nagasharmi, Jul 31, 2007 IP
  6. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if i do this:
    $Chains = mysql_db_query("4MCS","SELECT DISTINCT Chain_store,Shop FROM customer WHERE Chain_store != ' '");
    PHP:
    It does basically what i want it to do, but it puts EVERY store of every chain in the drop down box. I want one store from each chain in the box.
     
    Greenmethod, Jul 31, 2007 IP
  7. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ahh, I see. This is a common question.

    In that case, you want....

    SELECT DISTINCT Chain_store,MAX(Shop) FROM customer WHERE Chain_store != ' ' GROUP BY Chain");

    or

    SELECT DISTINCT Chain_store,MIN(Shop) FROM customer WHERE Chain_store != ' ' GROUP BY Chain");

    Both will give you one store from each chain - either the first alphabetically, or the last.
     
    ecentricNick, Jul 31, 2007 IP
  8. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks so much for your help! When I put these in, it comes up with an empty drop down box.. I took out the group by and still the same thing until I take out the min or max.. any suggestions? Is there a good page that I can read about this?

     
    Greenmethod, Jul 31, 2007 IP
  9. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #9
    oops, slight bug in my syntax....

    $Chains = mysql_db_query("4MCS","SELECT Chain_store,MAX(Shop) FROM customer GROUP BY Chain_Store HAVING Chain_store != ' ' ");
     
    ecentricNick, Jul 31, 2007 IP
  10. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    That actually worked when I took the MAX() out, and i'm not exactly sure why. I would think that if there isn't a distinct in there, it would pull all of the customers that are in a chain store, not just one.. can you explain this to me?

     
    Greenmethod, Jul 31, 2007 IP
  11. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #11
    The group by clause groups everything up, so you only get one row per grouped item.

    It's slightly different to distinct - mainly in terms of what it does with the rest of the columns.
     
    ecentricNick, Jul 31, 2007 IP