Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql python combine results

Tags:

python

sql

mysql

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 ()
like image 749
Cmag Avatar asked Jul 05 '26 08:07

Cmag


1 Answers

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.

like image 80
Alain Collins Avatar answered Jul 07 '26 00:07

Alain Collins