Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can "Distinct" key Word be used twice in a single Select Query?

Tags:

mysql

distinct

Can "Distinct" key Word be used twice in a single Select Query? like wise:

select DISTINCT(trackid), DISTINCT(table_name) 
from jos_audittrail 
where live = 0 AND operation = UPDATE

Thanks

like image 715
OM The Eternity Avatar asked Dec 05 '25 21:12

OM The Eternity


2 Answers

No, By Default Distinct works on all the columns you are selecting. eg.

select DISTINCT trackid, table_name 
from jos_audittrail 
where live = 0 AND operation = UPDATE

This will select all distinct trackid and table name combination

EDIT

For retrieving distinct records other than this you can use the answer given by davek. It will work.

You can use group by to do this work as group by is being applied on both the columns being provided so no aggregate function is needed.

    SELECT trackid, table_name FROM jos_audittrail 
    WHERE live = 0 AND operation = 'UPDATE' 
    GROUP BY trackid, tablename
like image 142
Shantanu Gupta Avatar answered Dec 07 '25 10:12

Shantanu Gupta


select trackid
, table_name
, count(*)
from jos_audittrail 
where live = 0 AND operation = UPDATE
group by trackid, table_name
order by trackid, table_name

would give you distinct combinations of the two.

like image 35
davek Avatar answered Dec 07 '25 10:12

davek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!