简体   繁体   English

为sql中的所有列选择Distinct值

[英]Select Distinct values for all the columns in sql

I am trying to execute this sql: 我正在尝试执行此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...... 现在,我想要的是不同的主题,不同的班级以及不同的book_name,然后继续……

But, as we know if I use distinct with this query as below: 但是,据我们所知,我是否对以下查询使用了distinct:

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. 它会导致所有select语句的输出完全不同。

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). 请不要建议对所有列使用不重复查询六次(一次用于Class,一次用于book_name,一次用于作者,等等)。

Kindly revert. 请还原。

You could use GROUP BY to get all the combinations 您可以使用GROUP BY获取所有组合

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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