distinct - MYSQL using distinction but including an exemption/exclusion condition? -
i using count query select non duplicate records table, i'm using following:
function check_profile_views3() { global $connection; global $_session; $query = "select count(distinct profile_id) totalcount2 ptb_profile_views viewed_profile_id=".$_session['user_id']." , profile_id!=".$_session['user_id'].""; $check_profile_views_set3 = mysql_query($query, $connection); confirm_query($check_profile_views_set3); return $check_profile_views_set3; }
my table looks this:
id | profile_id | viewed_profile_id 1 3 6 2 3 6 3 4 6 4 -1 6 5 -1 6
so in example query selects distinct values , doesnt count values duplicates in profile_id field,
however want add exception query count '-1' profile id's counting distinct positive numbers.
is possible im still learning sql greatful if can me out.
thanks
desired result
the mysql table goes this: id | profile_id | viewed_profile_id 1 3 6 2 3 6 3 4 6 4 -1 6 5 -1 6 this: end result pulls results table id | profile_id | viewed_profile_id 1 3 6 2 4 6 3 -1 6 4 -1 6
ok im trying jw's answer , im trying im going wrong somewhere:
this how im doing query:
function check_profile_views3() { global $connection; global $_session; $query = " select a.* ptb_profile_views inner join ( select profile_id, min(id) min_id ptb_profile_views profile_id > 0 group profile_id ) b on a.profile_id = b.profile_id , a.id = b.min_id union select * ptb_profile_views profile_id < 0"; $check_profile_views_set3 = mysql_query($query, $connection); confirm_query($check_profile_views_set3); return $check_profile_views_set3; }
and im calling on page so:
$check_profile_views_set3 = check_profile_views3(); while ($views3 = mysql_fetch_array($check_profile_views_set3)) { echo "". $views3['totalcount2'] ."";
can try this:
select viewed_profile_id,count(distinct profile_id) totalcount2 ptb_profile_views profile_id not in (".$_session['user_id']." ,-1) group viewed_profile_id
Comments
Post a Comment