SQL/MySQL/Select Clause/Distinct — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 10:16, 26 мая 2010
Содержание
Eliminating Duplicate Data Using DISTINCT 1
/*
mysql> SELECT DISTINCT Mark FROM Exam;
+------+
| Mark |
+------+
| 55 |
| 73 |
| 44 |
+------+
3 rows in set (0.00 sec)
*/
/* Prepare the data */
Drop TABLE Exam;
CREATE TABLE Exam (
StudentID INT NOT NULL,
ExamID INT NOT NULL,
Mark INT,
IfPassed SMALLINT
)TYPE = InnoDB;
/* Insert data for testing */
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (1,1,55,1);
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (1,2,73,0);
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (2,3,44,1);
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (1,4,55,1);
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (1,5,73,0);
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (2,6,44,1);
/* Real command */
SELECT DISTINCT Mark FROM Exam;
Eliminating Duplicate Data Using DISTINCT 2
/*
mysql> SELECT DISTINCT Mark, ExamID FROM Exam;
+------+--------+
| Mark | ExamID |
+------+--------+
| 55 | 1 |
| 73 | 2 |
| 44 | 3 |
| 55 | 4 |
| 73 | 5 |
| 44 | 6 |
+------+--------+
6 rows in set (0.00 sec)
*/
/* Prepare the data */
Drop TABLE Exam;
CREATE TABLE Exam (
StudentID INT NOT NULL,
ExamID INT NOT NULL,
Mark INT,
IfPassed SMALLINT
)TYPE = InnoDB;
/* Insert data for testing */
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (1,1,55,1);
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (1,2,73,0);
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (2,3,44,1);
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (1,4,55,1);
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (1,5,73,0);
INSERT INTO Exam (StudentID,ExamID,Mark,IfPassed) VALUES (2,6,44,1);
/* Real command */
SELECT DISTINCT Mark, ExamID FROM Exam;
Select distinct records using JOIN
/*
mysql> SELECT CONCAT_WS(" ", AuthorFirstName, AuthorMiddleName, AuthorLastName)
AS Author
-> FROM Authors
-> WHERE AuthID=
-> (
-> SELECT ab.AuthID
-> FROM AuthorArticle AS ab, Articles AS b
-> WHERE ab.ArticleID=b.ArticleID AND ArticleTitle="AI"
-> );
+-------------+
| Author |
+-------------+
| Annie Watts |
+-------------+
1 row in set (0.02 sec)
*/
Drop table Articles;
Drop table Authors;
Drop table AuthorArticle;
CREATE TABLE Articles (
ArticleID SMALLINT NOT NULL PRIMARY KEY,
ArticleTitle VARCHAR(60) NOT NULL,
Copyright YEAR NOT NULL
)
ENGINE=INNODB;
INSERT INTO Articles VALUES (12786, "How write a paper", 1934),
(13331, "Publish a paper", 1919),
(14356, "Sell a paper", 1966),
(15729, "Buy a paper", 1932),
(16284, "Conferences", 1996),
(17695, "Journal", 1980),
(19264, "Information", 1992),
(19354, "AI", 1993);
CREATE TABLE Authors (
AuthID SMALLINT NOT NULL PRIMARY KEY,
AuthorFirstName VARCHAR(20),
AuthorMiddleName VARCHAR(20),
AuthorLastName VARCHAR(20)
)
ENGINE=INNODB;
INSERT INTO Authors VALUES (1006, "Henry", "S.", "Thompson"),
(1007, "Jason", "Carol", "Oak"),
(1008, "James", NULL, "Elk"),
(1009, "Tom", "M", "Ride"),
(1010, "Jack", "K", "Ken"),
(1011, "Mary", "G.", "Lee"),
(1012, "Annie", NULL, "Peng"),
(1013, "Alan", NULL, "Wang"),
(1014, "Nelson", NULL, "Yin");
CREATE TABLE AuthorArticle (
AuthID SMALLINT NOT NULL,
ArticleID SMALLINT NOT NULL,
PRIMARY KEY (AuthID, ArticleID),
FOREIGN KEY (AuthID) REFERENCES Authors (AuthID),
FOREIGN KEY (ArticleID) REFERENCES Articles (ArticleID)
)
ENGINE=INNODB;
INSERT INTO AuthorArticle VALUES (1006, 14356),
(1008, 15729),
(1009, 12786),
(1010, 17695),
(1011, 15729),
(1012, 19264),
(1012, 19354),
(1014, 16284);
SELECT DISTINCT CONCAT_WS(" ", AuthorFirstName, AuthorMiddleName, AuthorLastName) AS Author
FROM Authors AS a JOIN AuthorArticle AS ab ON a.AuthID=ab.AuthID
JOIN Articles AS b ON ab.ArticleID=b.ArticleID
WHERE ArticleTitle="AI";
Use DISTICNT to get unique value
/*mysql> select * from employee;
+----+-----------+----------+----------------------------+------+---------------+--------+-------+---------------------+
| id | firstname | lastname | title | age | yearofservice| salary | perks | email |
+----+-----------+----------+----------------------------+------+---------------+--------+-------+---------------------+
| 1 | John | Chen | Senior Programmer | 31 | 3| 120000 | 25000 | j@hotmail.ru |
| 2 | Jan | Pillai | Senior Programmer | 32 | 4| 110000 | 20000 | g@yahoo.ru |
| 3 | Ane | Pandit | Web Designer | 24 | 3| 90000 | 15000 | a@gmail.ru |
| 4 | Mary | Anchor | Web Designer | 27 | 2| 85000 | 15000 | m@mail.ru |
| 5 | Fred | King | Programmer | 32 | 3| 75000 | 15000 | f@net.ru |
| 6 | John | Mac | Programmer | 32 | 4| 80000 | 16000 | j@hotmail.ru |
| 7 | Arthur | Sam | Programmer | 28 | 2| 75000 | 14000 | e@yahoo.ru |
| 8 | Alok | Nanda | Programmer | 32 | 3| 70000 | 10000 | a@yahoo.ru |
| 9 | Susan | Ra | Multimedia Programmer | 32 | 4| 90000 | 15000 | h@gmail.ru |
| 10 | Paul | Simon | Multimedia Programmer | 23 | 1| 85000 | 12000 | ps@gmail.ru |
| 11 | Edward | Parhar | Multimedia Programmer | 30 | 2| 75000 | 15000 | a@hotmail.ru |
| 12 | Kim | Hunter | Senior Web Designer | 32 | 4| 110000 | 20000 | kim@coolmail.ru |
| 13 | Roger | Lewis | System Administrator | 32 | 3| 100000 | 13000 | roger@mail.ru |
| 14 | Danny | Gibson | System Administrator | 31 | 2| 90000 | 12000 | danny@hotmail.ru |
| 15 | Mike | Harper | Senior Marketing Executive | 36 | 1| 120000 | 28000 | m@gmail.ru |
| 16 | Mary | Sunday | Marketing Executive | 31 | 5| 90000 | 25000 | monica@bigmail.ru |
| 17 | Jack | Sim | Marketing Executive | 27 | 1| 70000 | 18000 | hal@gmail.ru |
| 18 | Joe | Irvine | Marketing Executive | 27 | 1| 72000 | 18000 | joseph@hotmail.ru |
| 19 | Henry | Ali | Customer Service Manager | 32 | 3| 70000 | 9000 | shahida@hotmail.ru |
| 20 | Peter | Champion | Finance Manager | 32 | 2| 120000 | 25000 | peter@yahoo.ru |
+----+-----------+----------+----------------------------+------+---------------+--------+-------+---------------------+
20 rows in set (0.01 sec)
mysql> select distinct firstname from employee;
+-----------+
| firstname |
+-----------+
| John |
| Jan |
| Ane |
| Mary |
| Fred |
| Arthur |
| Alok |
| Susan |
| Paul |
| Edward |
| Kim |
| Roger |
| Danny |
| Mike |
| Jack |
| Joe |
| Henry |
| Peter |
+-----------+
18 rows in set (0.00 sec)
*/
Drop table employee;
CREATE TABLE employee (
id int unsigned not null auto_increment primary key,
firstname varchar(20),
lastname varchar(20),
title varchar(30),
age int,
yearofservice int,
salary int,
perks int,
email varchar(60)
);
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("John", "Chen", "Senior Programmer", 31, 3, 120000, 25000, "j@hotmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Jan", "Pillai", "Senior Programmer", 32, 4, 110000, 20000, "g@yahoo.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Ane", "Pandit", "Web Designer", 24, 3, 90000, 15000, "a@gmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Mary", "Anchor", "Web Designer", 27, 2, 85000, 15000, "m@mail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Fred", "King", "Programmer", 32, 3, 75000, 15000, "f@net.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("John", "Mac", "Programmer", 32, 4, 80000, 16000, "j@hotmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Arthur", "Sam", "Programmer", 28, 2, 75000, 14000, "e@yahoo.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Alok", "Nanda", "Programmer", 32, 3, 70000, 10000, "a@yahoo.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Susan", "Ra", "Multimedia Programmer", 32, 4, 90000, 15000, "h@gmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Paul", "Simon", "Multimedia Programmer", 23, 1, 85000, 12000, "ps@gmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Edward", "Parhar", "Multimedia Programmer", 30, 2, 75000, 15000, "a@hotmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Kim", "Hunter", "Senior Web Designer", 32, 4, 110000, 20000, "kim@coolmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Roger", "Lewis", "System Administrator", 32, 3, 100000, 13000, "roger@mail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Danny", "Gibson", "System Administrator", 31, 2, 90000, 12000, "danny@hotmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Mike", "Harper", "Senior Marketing Executive", 36, 1, 120000, 28000, "m@gmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Mary", "Sunday", "Marketing Executive", 31, 5, 90000, 25000, "monica@bigmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Jack", "Sim", "Marketing Executive", 27, 1, 70000, 18000, "hal@gmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Joe", "Irvine", "Marketing Executive", 27, 1, 72000, 18000, "joseph@hotmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Henry", "Ali", "Customer Service Manager", 32, 3, 70000, 9000, "shahida@hotmail.ru");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, email) values ("Peter", "Champion", "Finance Manager", 32, 2, 120000, 25000, "peter@yahoo.ru");
select * from employee;
select distinct firstname from employee;
Use DISTINCT to get non-dupliate records
/*
mysql> Drop table Bird;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE Bird (
-> name VARCHAR(20),
-> owner VARCHAR(20),
-> species VARCHAR(20),
-> sex CHAR(1),
-> birth DATE,
-> death DATE
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> INSERT INTO Bird VALUES ("BlueBird","Joe","Car","f","1999-03-30",NULL);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO Bird VALUES ("RedBird","Yin","Bus","m","1979-03-30",NULL);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Bird VALUES ("RedBird","Yin","Bus","m","1979-03-30",NULL);
Query OK, 1 row affected (0.00 sec)
mysql> select * from Bird;
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| BlueBird | Joe | Car | f | 1999-03-30 | NULL |
| RedBird | Yin | Bus | m | 1979-03-30 | NULL |
| RedBird | Yin | Bus | m | 1979-03-30 | NULL |
+----------+-------+---------+------+------------+-------+
3 rows in set (0.00 sec)
mysql> SELECT DISTINCT owner FROM Bird;
+-------+
| owner |
+-------+
| Joe |
| Yin |
+-------+
2 rows in set (0.00 sec)
*/
Drop table Bird;
CREATE TABLE Bird (
name VARCHAR(20),
owner VARCHAR(20),
species VARCHAR(20),
sex CHAR(1),
birth DATE,
death DATE
);
INSERT INTO Bird VALUES ("BlueBird","Joe","Car","f","1999-03-30",NULL);
INSERT INTO Bird VALUES ("RedBird","Yin","Bus","m","1979-03-30",NULL);
INSERT INTO Bird VALUES ("RedBird","Yin","Bus","m","1979-03-30",NULL);
select * from Bird;
SELECT DISTINCT owner FROM Bird;