I work on python. I have a query where I have 3 tables on my db.
I have connected to a db and from a mysql table pulled out all the rows from two columns (idnum,clientname) e.g. (1234,renolds), (1235,renolds2)
Then each idnum has a seperate table e.g. sample_divya_1234;sample_divya_1235, from each of these tables we need to take all emails and lnames. (Note: each idnum has many email and lname records)
All the clientnames taken in step 1 are present in another table sample_divya3, so for each clientname need to pull out fname e.g.(saha, renolds)
Now email,lname,fname must all get dropped into a new table sql_table1
Input Tables
Sample_divya1:
+-----+------------+---
| idnum | clientname |
+-------+------------+-
| 1234 | renold |
| 1235 | renold1 |
+-------+------------+
sample_divya_1234:
+-------------------+----------+
| email | lname |
+-------------------+----------+
| [email protected] | abcd |
| [email protected] | bcda |
+-------------------+----------+
**sample_divya_1235**
+------------------+-----------+
| email | lname |
+------------------+-----------+
| [email protected] | xyza |
| [email protected] | sai |
| [email protected] | klm |
+------------------+-----------+
sample_divya3:
+--------+------------+
| fname | clientname |
+--------+------------+
| saha | renold |
| hasini | renold1 |
+--------+------------+
PRG:
import pandas as pd
import pymysql
import pymysql.cursors
from sqlalchemy import create_engine
from time import time
import datetime
conn=pymysql.connect(CONNECTDETAILS)
query = "select idnum from sample_divya1"
cursor=conn.cursor()
cursor.execute(query)
data = cursor.fetchall()
cursor.execute("drop table if exists sql_table1")
sql_table = "create table sql_table1(email varchar(128),lname varchar(128),fname varchar(128))"
cursor.execute(sql_table)
for id in data:
cursor.execute("select A.idnum, B.fname, B.clientname,C.lname,C.email from (select idnum,clientname from sample_divya1)A cross join (select fname,clientname from sample_divya3 )B where A.clientname=B.clientname cross join (select email, lname from sample_divya_"+id[0]+")C where A.idnum =id")
data_1=cursor.fetchall()
conn.commit()
print data_1
cursor.executemany("insert into sql_table1 (email,lname,fname) values (?,?,?)",data_1)
conn.commit()
conn.commit()
Output required:
+------------------+----------+--------+
| email | lname | fname |
+------------------+----------+--------+
| [email protected] | abcd | saha |
| [email protected] | bcda | saha |
| [email protected] | xyza | hasini |
| [email protected] | sai | hasini |
| [email protected] | klm | hasini |
Mistake is in my highlighted part of my query
""" # cursor.execute("select B.fname,C.lname,C.email from (select idnum,clientname from sample_divya1)A cross join (select fname,clientname from sample_divya3 )B where A.clientname=B.clientname cross join (select email, lname from sample_divya_"+id[0]+")C where A.idnum = id")"""
Here on trail and got to know that ""Unknown column 'id' in 'where clause"" so can someone help in this where clause