1

I have a mail table that looks like this:

CREATE TABLE `mail` (
  `id` int(20) NOT NULL auto_increment,
  `userto` varchar(30) collate utf8_unicode_ci NOT NULL,
  `userfrom` varchar(30) collate utf8_unicode_ci NOT NULL,
  `message` longtext collate utf8_unicode_ci NOT NULL,
  `seen` int(1) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

I want to do something like this:

SELECT userfrom FROM mail
WHERE userto = "admin"

however, I don't want any repeats in the output. How would I do this?

Zim
  • 388
  • 1
  • 4
  • 15
  • 2
    possible duplicate of [How to select unique records by SQL](http://stackoverflow.com/questions/1641718/how-to-select-unique-records-by-sql) – Joe Mar 07 '15 at 06:44

4 Answers4

0

You have to use DISTINCT for non repeative record like

SELECT DISTINCT userfrom FROM mail
WHERE userto = "admin"

Check DISTINCT

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
0

I believe you need to use the DISTINCT keyword.

Useful link => http://www.w3schools.com/sql/sql_distinct.asp

kkaosninja
  • 1,301
  • 11
  • 21
0

You just make the column as unique

ALTER TABLE mail
ADD UNIQUE (userto)

Use distinct DISTINCT

SELECT DISTINCT  userfrom FROM mail WHERE userto = "admin"
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
0

You can use DISTINCT keyword for unique values of selected columns.

Example:-

SELECT DISTINCT userfrom FROM mail
WHERE userto = "admin"

HTH!

karthik manchala
  • 13,492
  • 1
  • 31
  • 55