2

im trying to select distinct, the column 'content' in my table reviews,

it works when i do this:

function get_new_reviews() {
            global $connection;
            global $_SESSION;
            $query = "SELECT DISTINCT r.content
                        FROM ptb_reviews r, ptb_profiles p
                        WHERE r.to_user_id =".$_SESSION['user_id']."
                        AND r.deleted = '0'
                        AND r.read_review = '0'
                        AND r.approved = '0'
                        AND r.from_user_id != '0'
                        ORDER BY r.date_added DESC 
                        LIMIT 0, 14";


                        $reviews_set = mysql_query($query, $connection);
            confirm_query($reviews_set);
            return $reviews_set;
        } 

but i also need columns

r.from_user_id, p.display_name, r.id reviews_id, r.date_added

and when i try and add them in it has lots of the same content going down the page, is there a way i can just select all fields in my table but only distinct select the content column?

thanks

6
  • 1
    At least one of those columns must be changing; as such, which of the multiple possible values do you want to select?
    – eggyal
    Commented May 1, 2013 at 11:21
  • 1
    i want to select all columns but set column 'content' to select distinct
    – Bear John
    Commented May 1, 2013 at 11:22
  • Have you tried GROUP BY that specific column? Commented May 1, 2013 at 11:23
  • 1
    Yes, I understand that... but for a given "distinct" value of content, say 'thisvalue', there must be more than one set of values for the other columns (otherwise you wouldn't observe the behaviour you describe). Therefore, which of those multiple possible values do you wish to select for each distinct value of content?
    – eggyal
    Commented May 1, 2013 at 11:24
  • 1
    @RolandoIsidoro: That will result in indeterminate values in the other (aka "hidden") columns, hence why I ask the OP to specify which particular values he seeks.
    – eggyal
    Commented May 1, 2013 at 11:24

1 Answer 1

0

I think that is what you are looking for mate:

SELECT *
FROM (
    SELECT r.content, r.from_user_id, p.display_name, r.id reviews_id, r.date_added
    FROM ptb_reviews r, ptb_profiles p
    WHERE r.to_user_id =".$_SESSION['user_id']."
        AND r.deleted = '0'
        AND r.read_review = '0'
        AND r.approved = '0'
        AND r.from_user_id != '0'
    GROUP BY r.content
    LIMIT 0, 14
) t1
ORDER BY date_added DESC 
2
  • That query gives unpredictable result and certainly shouldn't be used. Selecting non-aggregated columns that aren't in the group by statement is wrong 99,9% of time. Commented May 1, 2013 at 11:40
  • That doesn't change anything. You still can't predict what r.from_user_id will be returned for given r.content if there is more than one candidate. Commented May 1, 2013 at 12:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.