上次写的查询完成了一些基本功能,但是里面有许多bug,很多情况没有考虑周全:
首先看表结构:两个表,分别为details,world
由于世界疫情和中国疫情是分别爬取的,所以查询时要根据情况查询不同的表,上次的查询没有考虑到。
world里没有中国的数据,所以查询中国时要在details表里。
当查询时只填了“中国”,那么我们希望查出的是各省各日期的情况。当填了省份或市区时,我们才展示各市的情况。
由于details表结构的特殊,查市区与省份时比较容易,但是查中国时,我们不但要将各市区的数据加起来变为省的数据,还要按时间分开。
"select update_time,province,sum(confirm),sum(heal),sum(dead) from details where (update_time between ds and ds1 )group by province , update_time"
这就是基本的查询语句,但是根据不同的情况还要进行不同的修改:
if(province!="" or city!="" or country=="中国"): sql = "select update_time,province,sum(confirm),sum(heal),sum(dead) from details " "where (update_time between " + "'" + ds + "' and '" + ds1 + "'" + ") " if (province != "" and city ==""): sql="select update_time,province,city,confirm,heal,dead from details " "where (update_time between " + "'" + ds + "' and '" + ds1 + "'" + ") " sql = sql + "and (province like " + '"%' + province + '%") ' if (province!="" and city != ""): sql = "select update_time,province,city,confirm,heal,dead from details " "where (update_time between " + "'" + ds + "' and '" + ds1 + "'" + ") " sql = sql + "and (city like " + '"%' + city + '%") ' sql = sql + "and (province like " + '"%' + province + '%") ' if(country=="中国" and province=="" and city==""): sql=sql+"group by province , update_time" if(province=="" and city=="" and country!="中国"): sql = "select update_time,country,confirm,heal,dead,nowconfirm from world " "where (update_time between " + "'" + ds + "' and '" + ds1 + "'" + ") " if (continent != ""): sql = sql + "and (continent like " + '"%' + continent + '%") ' if (country != ""): sql = sql + "and (country like " + '"%' + country + '%") ' print("查询(带日期)") print(sql) res = query(sql)