简体   繁体   中英

mysql: select distinct column in table?

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

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM