Cmag
Cmag

Reputation: 15770

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 ()

Upvotes: 3

Views: 1608

Answers (1)

Alain Collins
Alain Collins

Reputation: 16362

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.

Upvotes: 4

Related Questions