简体   繁体   中英

Select Distinct values for all the columns in sql

I am trying to execute this sql:

select subject, class, book_name, author, publisher, publish_year from tb_books;

Now, what I want is distinct subject, as well as distinct class, as well as distinct book_name, and go on......

But, as we know if I use distinct with this query as below:

select distinct subject, class, book_name, author, publisher, publish_year from tb_books;

it will result in the complete distinct output of all the select statement.

Is their any way to find out all the distinct values. Please don't suggest to use the distinct query six times with all the columns (one for Class, one for book_name, one for author, etc).

Kindly revert.

You could use GROUP BY to get all the combinations

select subject, class, book_name, author, publisher, publish_year from tb_books
group by subject, class, book_name, author, publisher, publish_year;

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