I have two columns from a table; fromid and toid, which contain the same user id on occasions.
I have also a dropdown select box which selects all the ids from both these columns.
My problem is that the dropdown selects all users, but also duplicates when one user is in both columns.
What I would like is to select only a user once and give one of the columns a priority or MAX(is that it?) if the user appears in both columns. Here is the code I have so far:
$sql="
SELECT
DISTINCT u.".($config->realnames ? "name" : "username")." AS displayname,
u.id
FROM
(#__users AS u
INNER JOIN
#__uddeim AS um
ON
u.id=um.fromid)
WHERE
um.toid=".(int)$myself."
AND
um.totrash=0
AND
`um`.`delayed`=0".$filter."
UNION ALL
SELECT
DISTINCT u.".($config->realnames ? "name" : "username")." AS displayname,
u.id
FROM
(#__users AS u
INNER JOIN
#__uddeim AS um
ON
u.id=um.toid)
WHERE
um.fromid=".(int)$myself."
AND
um.totrashoutbox=0".$filter."
AND um.systemflag=0";
WHAT I’VE TRIED –
I tried using a count query but this dind’t work:
if (count ($row->displayname > 1))
echo "duplicate rows";
Try changing
UNION ALLin your query to justUNION. The former retains all rows from both datasets; the latter eliminates duplicates – it is a shortened form ofUNION DISTINCT.