Can I fetch data from multiple tables from mysql?

Can I fetch data from multiple tables from mysql ?

1 Like

HI Aditi,

MindsDB is built to take one table where you specify what column you want to learn to predict given all the other columns.

As such you can surely JOIN multiple tables in Mysql and learn from that.
MindsDB can take in a pandas data frame straight in, as such you can do

from sqlalchemy import create_engine

from mindsdb import Predictor

df = pd.read_sql('SELECT * FROM table1 LEFT JOIN table2 on .....', con=create_engine('mysql+pymysql://mysql_user:mysql_password@mysql_host/mysql_db'))

my_predictor = Predictor(name='My predictor from SQL example')
predictor.learn(to_predict='column_to_predict', from_data=df)
1 Like

To add to @Jorge_Torres 's reply:

We actually support a cleaner way to get that from mysql now using a mysql data source:

from mindsdb import MySqlDS, Predictor

mysql_ds = MySqlDS(query='SELECT * FROM table1 LEFT JOIN table2 on .....', host=HOST, user=USER, password=PASSWORD, database=DATABASE, port=PORT)

my_predictor = Predictor(name='my_predictor_from_sql_example')
predictor.learn(to_predict='column_to_predict', from_data=mysql_ds

However, in order for this to work, you need to install mysqlclient >= 1.4.0 or install mindsdb via pip install mindsdb[extra_data_sources] in order to get the dependencies for the extra datasource we currently support.

1 Like

@aditi.verma You can also check the tutorial about the usage of MindsDB and MySQL