开发者

mysql python combine results

trying to write the following in python more elegantly...from mysql perspective. You will notice I am trying to combine results in one table based upon the entries in another. What is the proper,clean, industry smart way of writing such SQL query?

Essentially, I would like to say this... "select all 开发者_开发知识库urls from urls table, which belong to some site group, in sites table"

Thanks!

site = sys.argv[0]
checksanity (log,site) #check syntax, etc

log.info ("Running site %s", site)
cursor = conn.cursor ()

#get siteid
query = "SELECT sites.id from sites WHERE sitename LIKE '" + site + "'"
cursor.execute (query)
siteidlong = cursor.fetchone()
siteid = str(siteidlong[0])

query = "SELECT search_for,urls.url FROM urls WHERE site_id LIKE '" + siteid + "'"
print query
cursor.execute (query)
resultstring = cursor.fetchall()
print resultstring

cursor.close ()
conn.close ()


Welcome to SQL. Now that you've written two queries, it's time to learn about JOIN and SQL injection.

select *
from urls, sites
where urls.site_id = sites.id
and sitename like ?

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜