简体   繁体   English

如何匹配列中的值?

[英]How to match values in a column?

I have a file temperature.txt with columns: 我有一个带有列的文件temperature.txt

city 
avghigh 
avglow 
coldmonth 
coldavghigh
coldavglow
warmmonth 
warmavghigh
warmavglow 

I need to return the names of the cities which have the same average low temperature. 我需要返回平均气温相同的城市的名称。 I also have this function: 我也有这个功能:

def run_query(db, q, args=None):
    conn = sqlite3.connect(db)
    cur = conn.cursor()
    if args is None:
        cur.execute(q)
    else:
        cur.execute(q, args)
    results = cur.fetchall()
    cur.close()
    conn.commit()
    conn.close()
    return results

all I got thus far (If it's correct is) 到目前为止,我所得到的(如果正确的话)

return run_query(noname.db, ('Select Cities, AvgLow from Table')

In SQL, this can be easily done using a self join on the table to get matching AvgLow for different cities like this: 在SQL中,可以使用表上的自我联接轻松地完成此操作,以针对不同的城市获得匹配的AvgLow,如下所示:

Select 
t.Cities, 
t.AvgLow         
from Table1 t
INNER JOIN Table1 t1 ON t.AvgLow = t1.AvgLow 
                    and t.Cities <> t1.Cities
ORDER BY t.AvgLow;

SQLite Demo SQLite演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM