Sorry. I am new to expressing SQL statement in Python.
I am trying to do "Select x .. " from previous result of query.
For example, I have a first query (sql1):
sql1 = 'select course_id, record_id from Courses ' \
' where ( record_id not in ("TEST", "TEST123")) '
cursor.execute(sql1)
e = cursor.fetachall()
When I tried to do it without breaking down queries, I got an error due to different data type.
Now, how do I go about selecting from sql1 (this time I have to add one calculation of data) ?
sql2 = ' select course_id, max(record_id) + 1 from ... ? '
make sql1 a subquery of sql2. For example:
"""select course_id, max(record_id) + 1 from
(select course_id, record_id from Courses
where record_id not in ('TEST', 'TEST123'))"""
You could use a view to store the query.
CREATE OR REPLACE VIEW v AS
select course_id, record_id from Courses
where ( record_id not in ("TEST", "TEST123"));
select course_id, max(record_id) + 1 from v;
With this you will need only one query with Python.
sql1 = 'CREATE OR REPLACE VIEW v AS
select course_id, record_id from Courses
where ( record_id not in ("TEST", "TEST123"));
select course_id, max(record_id) + 1 from v;'
cursor.execute(sql1)
e = cursor.fetachall()
More information about view: https://dev.mysql.com/doc/refman/5.7/en/create-view.html
Related
I have a table task_field where I store field headers as records that exist in another table product_wca as follows:
Now I have a query pulling data from the product_wca table, however I would like to select fields from this table that are in the table above as per 'name' field.
I'm not sure if this is possible to do in MYSQL alone as I cannot join on anything but hope it's possible with python, please see my failed attempt at this below where I tried to add a python for loop inside the mysql select statement:
Get list of fields from first table
mycursor = mydb.cursor()
mycursor.execute("SELECT name" \
" FROM `wfi-workflow`.task_field " \
"where " \
" taskid = 50 " \
" and model = 'wca'")
My failed attempt to show select fields from another table where available in table above:
newcursor = mydb.cursor()
mycursor.execute("select distinct portfolio.id," \
"portfolio.secid," \
for x in mycursor:
for i in x:
"Wca.'+i+', " \
"portfolio.code " \
"from portfolio_identifier portfolio " \
"left join product_wca WCA on portfolio.secid = WCA.secid " \
"where portfolio.account_id = 319 limit 1")
Is it possible to do what I'm attempting or another work around to only select fields from one table where they exist in another?
You can't put a for loop in the middle of an expression. Use the str.join() method to create a string containing the concatenated results.
mycursor = mydb.cursor()
mycursor.execute("""SELECT name
FROM `wfi-workflow`.task_field
where
taskid = 50
and model = 'wca'""")
names = ",".join("wca."+row[0] for row in mycursor)
mycursor.execute("""select distinct portfolio.id,
portfolio.secid,""" +str(names)+ """, portfolio.code
from portfolio_identifier portfolio
left join product_wca WCA on portfolio.secid = WCA.secid
where portfolio.account_id = 319 limit 1""")
I am trying to write a SELECT query with a WHERE condition and ORDER BY.
I have the query in MySQL and would like to convert it to Python.
This is my Mysql-Query:
SELECT StoreId
, ProductCode
, TotalQuantity
FROM storeinvoicedetails
WHERE StoreId=1
ORDER BY ProductCode
This is what I tried in Python:
sql = "SELECT StoreId,ProductCode,TotalQuantity FROM storeinvoicedetails \
WHERE StoreId = '%d'" % (1) \
"ORDER BY '%s' '%s'" % ('ProductCode','ASC')
query = """SELECT StoreId,ProductCode,TotalQuantity FROM storeinvoicedetails WHERE StoreId={0} ORDER BY ProductCode ASC""".format(store_id)
> sql = "SELECT StoreId,ProductCode,TotalQuantity FROM storeinvoicedetails \
WHERE StoreId = '%d' \
ORDER BY ProductCode ASC" % id
This should work.
STOREID = 1
# make sure you add the last space for every line, otherwise it will look like 'TotalQuantityFROM' causing an error
myQuery = "SELECT StoreId,ProductCode,TotalQuantity " +
"FROM storeinvoicedetails " +
"WHERE StoreId=" + str(STOREID) + " "
"ORDER BY ProductCode"
I have bottom MySql query (sql1).
sq1 = 'select course_id, creator_id, max(course_num) + 1, recordid
' from Courses where recordid in' \
' (' + ','.join(map(str, RecordMatch1)) + ') group by recordid'
cursor.execute(sql1)
BTW, RecordMatch1 is an object that has matching data from other previous queries.
I am trying to see if this is possible; (select * from sql1) portion.
sql2 = ' insert into Courses (course_id, creator_id, course_num, record_id) '\
' Values ( select * from sql1)'
cursor.execute(sql2)
Or do I have to express everything rather than using (Select * )?
What is best practice?
You can do this, but you should specify columns in case of schema changes.
Just need to confirm you are trying to run a select query and insert its output to a insert query. If that is the case this appears to be good.
yes, you can but you should do something like
sql = "SELECT course_id, creator_id, course_num, record_id FROM Courses"
all = cursor.fetchall()
for i in range(len(all))
sql1 = "INSERT INTO Courses (course_id, creator_id, course_num, record_id) VALUES (%s, %s, %s, %s)"
cursor.execute(sql1, (all[i]['Key'], all[i]['Key2'], all[i]['Key3'], all[i]['Key3']))
you can change the select like you want, remember that return a dictionary so take care about the keys, add print(all) to see what happen with the select and see the keys of each column
I have this Python code:
def get_employees(conditions, fields):
cursor.execute("SELECT employeeID FROM employees WHERE name=%s, budget=%s,
%year=%s,(some of conditions))
Is there any way to get employeeIDs if I set in conditions not all parameters, etc. only name and year?
If conditions were a dictionary, you could construct a query string:
def get_employees(conditions):
query = 'select employeeid from employees'
if conditions:
query += ' where ' + ' and '.join(key + ' = %s' for key in conditions.keys())
cursor.execute(query, conditions.values())
(I should note that here I am assuming that conditions does not have user-supplied keys. If there are user-supplied keys, this is definitely vulnerable to SQL injection.)
usually it is done via dynamic sql building, like this:
sql = "SELECT employeeID FROM employees WHERE 1=1";
if condition has name
sql += " and name='" .escape(name) . "'"
if condition has somefield
sql += " and somefield='" .escape(somefield) . "'"
etc
execute final sql
Lets say I'm having 4 tables 'A(id, type, protocol), B(id, A_id, info), C(id, B_id, details) and D(id, C_id, port_info). Table A and Table B are connected via foreign key id from Table A and A_id from Table B. Similarly, Table B and TableC are connected via foreign key id from TableB and B_id from Table C, and in the same way , Table C and Table D are also connected.
Now, I want to get port_info from Table D of all the protocols from Table A.
I know one method whose time complexity is O(n^4), which I'm using currently. The method is as follow :
db = MySQLdb.connect(host="localhost", user="root", passwd="", db="mydb")
cur = db.cursor()
cur.execute("SELECT * FROM A")
A_results = cur.fetchall()
for A_row in A_results :
id = A_row[0]
cur.execute("SELECT * FROM B WHERE A_id = %d " % (id ))
B_results = cur.fetchall()
for B_row in B_results :
id = B_row[0]
cur.execute("SELECT * FROM C WHERE B_id = %d " % (id ))
c_results = cur.fetchall()
for C_row in C_results :
id = C_row[0]
cur.execute("SELECT * FROM D WHERE C_id = %d " % (id ))
D_results = cur.fetchall()
for D_row in D_results :
print "Port = " + str(port)
But this method takes O(n^4), so is there any efficient way in terms of time complexity , that can solve this problem.
Your suggestions are highly appreciated.
Execute it in a single JOIN query and let MySQL do the necessary optimizations while handling large data sets (which, after all, is what the database is best at), providing your application with a single result set. The query looks like this:
SELECT A.protocol, D.port_info
FROM A JOIN B ON A.id = B.A_id
JOIN C ON B.id = C.B_id
JOIN D ON C.id = D.C_id
ORDER BY protocol
...and then use your cursor to go through that single resultset.