1

I Have a private message system on my site and i'm trying to pull all subject's on the messages. I need to show any subject once.

so if I have this subjects:

hey
hello
hey
good morning
good morning

I need to print this:

hey
hello
good morning

I can just cross it out with if, but I guess there is a better way with sql.

Thank you.

3 Answers3

1

Try something like:

SELECT DISTINCT subject FROM emails;
Dwayne Towell
  • 8,154
  • 4
  • 36
  • 49
1

Option 1 : DISTINCT

SELECT DISTINCT subject 
FROM my_table

Option 2 : GROUP BY

 SELECT subjects
 FROM my_table
 GROUP BY subjects

Difference between GROUP BY and DISTINCT

Distinct is used to filter unique records out of the records that satisfy the query criteria.

Group by clause is used to group the data upon which the aggregate functions are fired and the output is returned based on the columns in the group by clause. It has its own limitations such as all the columns that are in the select query apart from the aggregate functions have to be the part of the Group by clause.

Community
  • 1
  • 1
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
  • 1
    Great! The explanation is what made this answer a good answer (and different from all the others here). – thpl Feb 24 '14 at 08:48
0

If you use SQL.

select distinct subjects
  from YOUR_TABLE.

or

Select subjects
  from YOU_TABLE
group by subjects
Rafa Hernández
  • 468
  • 7
  • 19
  • 1
    Please add a little information about when to use `GROUP BY` and when to use `DISTINCT`. Your answer implies that these two are the same which is not quite true. – thpl Feb 24 '14 at 08:33
  • Distinct and group by are similar but not equals. For instance. In a table with 1000 records with 100 repetitions and using a function, the most difference is performace. select distinc FUNCTION(COLUMN) from table return 100 records and evaluate the function 1000 times once per record. select FUNCTION(COLUMN) from TABLE group by FUNCTION(COLUMN) are equal. – Rafa Hernández Feb 24 '14 at 08:37
  • But select FUNCTION(COLUMN) from TABLE group by COLUMN Return 100 records and evaluate only 100 times the function. – Rafa Hernández Feb 24 '14 at 08:44