您的位置:首页 > 其它

leetcode Database1(三)

2015-07-04 17:12 218 查看
一、RisingTemperature

Givena
Weather
table,writeaSQLquerytofindalldates'Idswithhighertemperaturecomparedtoitsprevious(yesterday's)dates.

+---------+------------+------------------+
|Id(INT)|Date(DATE)|Temperature(INT)|
+---------+------------+------------------+
|1|2015-01-01|10|
|2|2015-01-02|25|
|3|2015-01-03|20|
|4|2015-01-04|30|
+---------+------------+------------------+

Forexample,returnthefollowingIdsfortheaboveWeathertable:

+----+
|Id|
+----+
|2|
|4|
+----+
分析:意思就是在Weather表中,写一个SQL查询与前一天相比温度更高的日期对应的ID。
代码:


#WriteyourMySQLquerystatementbelow SELECTw1.Id FROMWeatherw1JOINWeatherw2ONTO_DAYS(w1.Date)=TO_DAYS(w2.Date)+1Andw1.Temperature>w2.Temperature;

 其中,TO_DAYS(date)给定一个日期date,返回一个天数(从年份0开始的天数) 

其他解法:

SELECTw1.IdFROMWeatherw1,Weatherw2WHEREdateDiff(w1.Date,w2.Date)=1ANDw1.Temperature>w2.Temperature;

  其中,dateDiff()函数返回两个日期之间的天数。

还有这样的方式:

date_add(w1.date,interval1day)=w2.date

w2.Date=DATE_SUB(w1.Date,INTERVAL1DAY)

二、DeleteDuplicateEmails

WriteaSQLquerytodeleteallduplicateemailentriesinatablenamed
Person
,keepingonlyuniqueemailsbasedonitssmallestId.

+----+------------------+
|Id|Email|
+----+------------------+
|1|john@example.com|
|2|bob@example.com|
|3|john@example.com|
+----+------------------+
Idistheprimarykeycolumnforthistable.

Forexample,afterrunningyourquery,theabove
Person
tableshouldhavethefollowingrows:

+----+------------------+
|Id|Email|
+----+------------------+
|1|john@example.com|
|2|bob@example.com|
+----+------------------+

分析:意思就是删除Email列中重复项所在的行,而且保留的不重复行Id更小。

代码:

#WriteyourMySQLquerystatementbelow DELETEp1 FROMPersonp1,Personp2 WHEREp1.Email=p2.EmailANDp1.Id>p2.Id

 其他解法:

DELETEFROMPerson WHEREIdIN (SELECTP1.IdFROMPersonASP1,PersonASP2 WHEREP1.Id>P2.IdANDP1.Email=P2.Email);

报错:RuntimeErrorMessage:Youcan'tspecifytargettable'Person'forupdateinFROMclause

 所以得注意:Inmysqlyoumust'tupdateatablewhileusingselectclause,Youcanonlydothatstepbystep.However,youcanuseamiddletableas: 

deletefromPersonwhereidnotin(selectt.idfrom(selectmin(id)asidfromPersongroupbyemail)t)

 或:

MySQLDon'tallowreferringdeletetargettableinsubquery,aworkaroundisuse(select*fromPerson)togetanewtable.

deletefromPersonwhereIdin(selectp1.Idfrom(select*fromPerson)p1,(select*fromPerson)p2wherep1.Email=p2.Emailandp1.Id>p2.Id)

另外ps:刚开始也想过用"SELECTDISTINCTEmailfromPerson",但是注意到:DeleteandDistinctarecompletelydifferent,whiledeletealtersthetable,distinctonlyselectsdistinctvaluesanddoesn'taltertable. 所以这样是不可行的!

三、CustomersWhoNeverOrder

Supposethatawebsitecontainstwotables,the
Customers
tableandthe
Orders
table.WriteaSQLquerytofindallcustomerswhoneverorderanything.

Table:
Customers
.

+----+-------+
|Id|Name|
+----+-------+
|1|Joe|
|2|Henry|
|3|Sam|
|4|Max|
+----+-------+


Table:
Orders
.

+----+------------+
|Id|CustomerId|
+----+------------+
|1|3|
|2|1|
+----+------------+


Usingtheabovetablesasexample,returnthefollowing:

+-----------+
|Customers|
+-----------+
|Henry|
|Max|
+-----------+
分析:题意为假设一个网站包含两个表,顾客表Customers和订单表Orders。编写一个SQL查询找出所有从未下过订单的顾客。
思路:使用NOTIN,NOTEXISTS,或者LEFTJOIN都是可以解决的。
解法一:
NOTIN


#WriteyourMySQLquerystatementbelow SELECTName FROMCustomersCWHEREC.Idnotin(selectO.CustomerIdfromOrdersO);


 解法二:

NOTEXISTS


#WriteyourMySQLquerystatementbelow SELECTNameFROMCustomerscWHERENOTEXISTS(SELECTCustomerIdFROMOrdersoWHEREo.CustomerId=c.id);

 解法三:

SELECTC.Name FROMCustomersASCLEFTOUTERJOINOrdersASO ONC.Id=O.CustomerId WHEREO.CustomerIdISNULL;

  




 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: