Using the distinct function in SQL

前端 未结 18 924
無奈伤痛
無奈伤痛 2021-02-06 16:55

I have a SQL query I am running. What I was wanting to know is that is there a way of selecting the rows in a table where the value in on one of those columns is distinct? When

18条回答
  •  余生分开走
    2021-02-06 17:42

    What is the question your query is trying to answer?

    Do you need to know which classes have only one teacher?

    select class_name, count(teacher) 
    from class group by class_name having count(teacher)=1
    

    Or are you looking for teachers with only one student?

    select teacher, count(student) 
    from class group by teacher having count(student)=1
    

    Or is it something else? The question you've posed assumes that using DISTINCT is the correct approach to the query you're trying to construct. It seems likely this is not the case. Could you describe the question you're trying to answer with DISTINCT?

提交回复
热议问题