+ Reply to Thread
Results 1 to 6 of 6

Thread: MySQL distinct keyword - How do you retrieve each unique value?

  1. #1
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    MySQL distinct keyword - How do you retrieve each unique value?

    I was looking at the distinct keyword in the SQL statement and I was wondering how you would get each individual columns. My guess was this:
    PHP Code:
    $sth $dbh->query("SELECT distinct colName FROM table");
    $i=0;
    while(
    $row=$sth->fetch()) {
          
    $colVal.$i $row[$i];

          echo 
    $colVal.$i;
          
    $i++;

    But I'm not sure that is quite right, or there may be a better way I'm just not seeing.
    Last edited by as4s1n; 04-17-2010 at 04:19 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: MySQL distinct keyword - How do you retrieve each unique value?

    If you have a column named 'city',

    $sth = $dbh->query("SELECT distinct city FROM address_book");

    will return just a list of city names, one entry per city. If you have five friends from Cairo, you get one entry for Cairo.

    There is no information from other columns, since that would not make any sense.
    Nothing is always absolutely so.

  3. #3
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: MySQL distinct keyword - How do you retrieve each unique value?

    Yes, but how do you display values onto the page? The way I did it?
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: MySQL distinct keyword - How do you retrieve each unique value?

    while($row=$sth->fetch()) {


    echo $row[ 0 ] ;

    }
    Nothing is always absolutely so.

  5. #5
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: MySQL distinct keyword - How do you retrieve each unique value?

    Oh, that makes sense. Sorry, I thought it was much more complicated.
    Last edited by as4s1n; 04-17-2010 at 04:46 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  6. #6
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: MySQL distinct keyword - How do you retrieve each unique value?

    Yes (guess you should add some <br /> etc formatting).

    Each row contains one result.
    Nothing is always absolutely so.

+ Reply to Thread

Similar Threads

  1. [OFF] Keyword Research Tool Without Monthly Fees.
    By kentzy99 in forum The Marketplace
    Replies: 2
    Last Post: 07-28-2009, 09:46 PM
  2. JavaScript 'this' keyword
    By nightbandit in forum Programming Help
    Replies: 2
    Last Post: 04-24-2009, 05:21 PM
  3. Cannot retrieve account type
    By rayvyn in forum Free Hosting
    Replies: 3
    Last Post: 09-30-2008, 10:14 AM
  4. Keyword Script - 650 points
    By ƒorte in forum The Marketplace
    Replies: 3
    Last Post: 11-17-2006, 08:12 PM
  5. Retrieve my ftp info
    By justhk in forum Free Hosting
    Replies: 0
    Last Post: 10-28-2005, 02:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers