leetcode182. Duplicate Emails
create table if not exists Person_182 (
Id int(10) not null auto_increment,
Email varchar(20) default null,
primary key(Id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8
insert into Person_182(Email) values ('[email protected]');
insert into Person_182(Email) values ('[email protected]');
insert into Person_182(Email) values ('[email protected]');
insert into Person_182(Email) values ('[email protected]');
insert into Person_182(Email) values ('[email protected]');
insert into Person_182(Email) values ('[email protected]');
select * from Person_182 order by Email;
2。答案
說明:提交時需要將Person_182修改為Person
SELECT Email
FROM Person_182
GROUP BY Email
HAVING COUNT(*) > 1;
SELECT Email
FROM Person_182
GROUP BY Email
HAVING COUNT(Id) > 1;
SELECT Email
FROM Person_182
GROUP BY Email
HAVING COUNT(Email) > 1;
SELECT distinct a.Email
FROM Person_182 a,Person_182 b
WHERE a.Id > b.Id
AND a.Email=b.Email
#Use self join
SELECT DISTINCT a.Email
FROM Person_182 a JOIN Person_182 b
ON (a.Email = b.Email)
WHERE a.Id <> b.Id
SELECT DISTINCT a.Email
FROM Person_182 a
WHERE EXISTS(
SELECT 1
FROM Person_182 b
WHERE a.Email = b.Email
LIMIT 1, 1
)
練習
建立熟悉的訂單表
create table if not exists Orders (
O_Id int(10) not null auto_increment,
OrderDate date default null,
OrderPrice int(20),
Customer varchar(20) default null,
primary key(O_Id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8
insert into Orders(OrderDate, OrderPrice, Customer) values ('2008/12/29', 1000, 'Bush');
insert into Orders(OrderDate, OrderPrice, Customer) values ('2008/12/29', 1600, 'Carter');
insert into Orders(OrderDate, OrderPrice, Customer) values ('2008/12/29', 700, 'Bush');
insert into Orders(OrderDate, OrderPrice, Customer) values ('2008/12/29', 300, 'Bush');
insert into Orders(OrderDate, OrderPrice, Customer) values ('2008/12/29', 2000, 'Adams');
insert into Orders(OrderDate, OrderPrice, Customer) values ('2008/12/29', 100, 'Carter');
select * from Orders order by Customer;
需求1:我們希望查詢每個客戶的總金額(總訂單)。我們想要使用 GROUP BY 語句對客戶進行組合。
SELECT Customer,SUM(OrderPrice)
FROM Orders
GROUP BY Customer
需求2:我們希望查詢訂單總金額少於 2000 的客戶。
在 SQL 中增加 HAVING 子句原因是,WHERE 關鍵字無法與合計函式一起使用。
SELECT Customer,SUM(OrderPrice)
FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
需求3:希望查詢客戶 “Bush” 或 “Adams” 擁有超過 1500 的訂單總金額。
我們在 SQL 語句中增加了一個普通的 WHERE 子句:
SELECT Customer,SUM(OrderPrice)
FROM Orders
WHERE Customer='Bush' OR Customer='Adams'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
需求4:將Orders表按照OrderPrice降序(升序)後選擇前5個元素?
select * from Orders order by OrderPrice asc limit 5;
select * from Orders order by OrderPrice desc limit 5;
需求5:將將Orders表按照OrderPrice降序後從第3條記錄開始選擇4條記錄?
select * from Orders order by OrderPrice asc limit 2, 4
解釋:
我們知道,在ms sql server中或access中,
若要查詢前10條記錄,使用top 10即可,
但在mysql中不支援這個寫法,它用limit 10。
我們可以利用MySQL中SELECT支援的一個子句——LIMIT——來完成這項功能。
LIMIT可以實現top N查詢,也可以實現M至N(某一段)的記錄查詢,具體語法如下:
SELECT * FROM MYTABLE
ORDER BY AFIELD
LIMIT offset, recnum
其中offset為從第幾條(M+1)記錄開始,recnum為返回的記錄條數。例:
select * from mytable
order by afield
limit 2, 5
即意為從第3條記錄開始的5條記錄。
建立預設的日期時間型別。
OrderDate datetime NOT NULL DEFAULT NOW(),
CREATE TABLE OrdersTestxx
(
OrderId int NOT NULL auto_increment,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (OrderId)
)
相關推薦
leetcode182. Duplicate Emails
create table if not exists Person_182 ( Id int(10) not null auto_increment, Email varchar(20) default null, primary
sql leetcode -Duplicate Emails
code etc 作用 region div 這樣的 技術 logs com 第一種解法: select distinct p1.Email as Email from Person p1, Person p2 where p1.Email=p2.Email an
[LeetCode] Delete Duplicate Emails
rom log john tab bsp bob ping leetcode email Write a SQL query to delete all duplicate email entries in a table named Person, keeping onl
182. Duplicate Emails--solution
query name table div tab find AI ould ble Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id |
182. Duplicate Emails
Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Email | +----+---------+ | 1 | [emai
LeetCode-查詢重複的電子郵箱(duplicate-emails)
題目難度: 簡單 編寫一個 SQL 查詢,查詢 Person 表中所有重複的電子郵箱。 示例: +----+---------+ | Id | Email | +----+---------+ | 1 | [email protected]
LeetCode Delete Duplicate Emails
Problem Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based
LeetCode Duplicate Emails
Problem Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Email | +----+-
[LeetCode] Delete Duplicate Emails 刪除重複郵箱
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. +----+------------
Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smalles
LeetCode-Algorithms #007 Reverse Integer, Database #182 Duplicate Emails
LeetCode-Algorithms #007 Reverse Integer 給定一個32位整數, 將其各位反轉並返回, 如果結果超出取值範圍就返回0 1 class Solution { 2 public int reverse(int x) { 3 //對原數取絕對值
leetcode 196. Delete Duplicate Emails(SQL,刪除重複元組)39
貼原題: 解析: 本題是讓從Person表中刪除Email重複的元組。 那麼可以用自身連線寫法和子查詢寫法兩種。 自身連線即需要找出Email相等,Id最小的元組。 子查詢
leetcode 196. Delete Duplicate Emails 刪除重複的電子郵箱 mySQL (delete where group order)
# Write your MySQL query statement below # First method DELETE p1 FROM Person p1, Person p2 WHERE p1
leetcode196-Delete Duplicate Emails(刪除重複並且id較大的資料)
問題描述: Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its sma
leetcode 182. Duplicate Emails
題意:查找出現一次以上的郵箱 select distinct x.Email /*這裡主要要加上distinct*/ from Person as x, Person as y where x
[MySQL]LeetCode196 Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. +
leetcode 196. Delete Duplicate Emails delete
題意:刪除Email重複的行,保留Id最小的行,也就使每個Email只保留Id最小那個 思路:用刪除語句進行刪除 delete p1 from Person as p1, Person as
196. Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest I
【Leetcode】196. Delete Duplicate Emails (Easy)
1.題目Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.翻
Aptana Studion出現 duplicate location重復定位報錯
alt 出現 重新啟動 sar nis http ava 安裝 src 1、下載SVN的時候出現報錯 duplicate location 2、點擊“available software sites”查看已可用的軟件網站 3、在這裏可以查看到SVN,勾選SVN復選