Results 1 to 7 of 7

Thread: Why does this SELECT DISTINCT querie not work?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Why does this SELECT DISTINCT querie not work?

    Hi Guys,

    For some reason this isnt working and wont display any results...

    Any ideas?
    <?
    $query1 = mysql_query ( "SELECT DISTINCT `artist` FROM `Artists` WHERE `Genre1` = '$usr->Genre1' OR `Genre2` = '$usr->Genre1' OR `Genre3` = '$usr->Genre1' OR `Genre1` = '$usr->Genre2' OR `Genre2` = '$usr->Genre2' OR `Genre3` = '$usr->Genre2' OR `Genre1` = '$usr->Genre3' OR `Genre2` = '$usr->Genre3' OR `Genre3` = '$usr->Genre3' ORDER BY RAND() LIMIT 0,1" );
    while ( $art = mysql_fetch_object ( $query1 ) )
    {
    ?>


    Basically, I need it to select an artist and it will display the artist picture, but at the moment it sometimes shows the same artist more than once.

    Regards,
    Jamie

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Why does this SELECT DISTINCT querie not work?

    Any one able to help?

  3. #3
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Why does this SELECT DISTINCT querie not work?

    When you need a distinct object that when you use "where" if you just want to find everything you use

    php Code:
    1. $query1 = mysql_query ( "SELECT`*` FROM `Artists`

    for distinct select use

    php Code:
    1. $query1 = mysql_query ( "SELECT `artist` FROM `Artists` WHERE `Genre1` = '$usr->Genre1' OR `Genre2` = '$usr->Genre1' OR `Genre3` = '$usr->Genre1' OR `Genre1` = '$usr->Genre2' OR `Genre2` = '$usr->Genre2' OR `Genre3` = '$usr->Genre2' OR `Genre1` = '$usr->Genre3' OR `Genre2` = '$usr->Genre3' OR `Gen

    That is remove the "DISTINCT" from the select statement.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Why does this SELECT DISTINCT querie not work?

    Wouldn't your sql just return 1 record everytime by its "LIMIT 0,1"?

    @Nightwalker83: How would that return a DISTINCT record?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Why does this SELECT DISTINCT querie not work?

    Quote Originally Posted by dee-u View Post
    How would that return a DISTINCT record?
    Isn't a distinct value just a matter of using "Where" in the search?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Why does this SELECT DISTINCT querie not work?

    No, distinct would return unique records. There could be 2 or more records returned by a query that satisfies a WHERE clause and using DISTINCT would eliminate the duplicate records. We could also just use GROUP BY which is the same as using DISTINCT.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Why does this SELECT DISTINCT querie not work?

    Quote Originally Posted by dee-u View Post
    No, distinct would return unique records. There could be 2 or more records returned by a query that satisfies a WHERE clause and using DISTINCT would eliminate the duplicate records. We could also just use GROUP BY which is the same as using DISTINCT.
    Ah ok! I just looked at a Java example of mine dealing with coloured pencils and it uses.

    java Code:
    1. "SELECT colour, size "+
    2.             "FROM tblPencils " +
    3.             "WHERE colour = ? "+
    4.             "ORDER BY colour";

    by as you say that example is using "ORDER BY".
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width