- Python 数据访问教程
- Python 数据访问 - 首页
- Python MySQL
- Python MySQL - 简介
- Python MySQL - 数据库连接
- Python MySQL - 创建数据库
- Python MySQL - 创建表
- Python MySQL - 插入数据
- Python MySQL - 选择数据
- Python MySQL - Where 子句
- Python MySQL - Order By
- Python MySQL - 更新表
- Python MySQL - 删除数据
- Python MySQL - 删除表
- Python MySQL - Limit
- Python MySQL - 连接
- Python MySQL - 游标对象
- Python PostgreSQL
- Python PostgreSQL - 简介
- Python PostgreSQL - 数据库连接
- Python PostgreSQL - 创建数据库
- Python PostgreSQL - 创建表
- Python PostgreSQL - 插入数据
- Python PostgreSQL - 选择数据
- Python PostgreSQL - Where 子句
- Python PostgreSQL - Order By
- Python PostgreSQL - 更新表
- Python PostgreSQL - 删除数据
- Python PostgreSQL - 删除表
- Python PostgreSQL - Limit
- Python PostgreSQL - 连接
- Python PostgreSQL - 游标对象
- Python SQLite
- Python SQLite - 简介
- Python SQLite - 建立连接
- Python SQLite - 创建表
- Python SQLite - 插入数据
- Python SQLite - 选择数据
- Python SQLite - Where 子句
- Python SQLite - Order By
- Python SQLite - 更新表
- Python SQLite - 删除数据
- Python SQLite - 删除表
- Python SQLite - Limit
- Python SQLite - 连接
- Python SQLite - 游标对象
- Python MongoDB
- Python MongoDB - 简介
- Python MongoDB - 创建数据库
- Python MongoDB - 创建集合
- Python MongoDB - 插入文档
- Python MongoDB - 查找
- Python MongoDB - 查询
- Python MongoDB - 排序
- Python MongoDB - 删除文档
- Python MongoDB - 删除集合
- Python MongoDB - 更新
- Python MongoDB - Limit
- Python 数据访问资源
- Python 数据访问 - 快速指南
- Python 数据访问 - 有用资源
- Python 数据访问 - 讨论
Python MySQL - 连接
当您将数据分成两个表时,您可以使用连接从这两个表中获取组合记录。
示例
假设我们创建了一个名为 EMPLOYEE 的表,并在其中填充了如下所示的数据:
mysql> CREATE TABLE EMPLOYEE( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT, CONTACT INT ); Query OK, 0 rows affected (0.36 sec) INSERT INTO Employee VALUES ('Ramya', 'Rama Priya', 27, 'F', 9000, 101), ('Vinay', 'Bhattacharya', 20, 'M', 6000, 102), ('Sharukh', 'Sheik', 25, 'M', 8300, 103), ('Sarmista', 'Sharma', 26, 'F', 10000, 104), ('Trupthi', 'Mishra', 24, 'F', 6000, 105); Query OK, 5 rows affected (0.08 sec) Records: 5 Duplicates: 0 Warnings: 0
然后,如果我们创建了另一个表并填充了它,如下所示:
CREATE TABLE CONTACT( ID INT NOT NULL, EMAIL CHAR(20) NOT NULL, PHONE LONG, CITY CHAR(20) ); Query OK, 0 rows affected (0.49 sec)
INSERT INTO CONTACT (ID, EMAIL, CITY) VALUES (101, '[email protected]', 'Hyderabad'), (102, '[email protected]', 'Vishakhapatnam'), (103, '[email protected]', 'Pune'), (104, '[email protected]', 'Mumbai'); Query OK, 4 rows affected (0.10 sec) Records: 4 Duplicates: 0 Warnings: 0
以下语句检索组合这两个表中的值的组合数据:
mysql> SELECT * from EMPLOYEE INNER JOIN CONTACT ON EMPLOYEE.CONTACT = CONTACT.ID; +------------+--------------+------+------+--------+---------+-----+--------------------+-------+----------------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | CONTACT | ID | EMAIL | PHONE | CITY | +------------+--------------+------+------+--------+---------+-----+--------------------+-------+----------------+ | Ramya | Rama Priya | 27 | F | 9000 | 101 | 101 | [email protected] | NULL | Hyderabad | | Vinay | Bhattacharya | 20 | M | 6000 | 102 | 102 | [email protected] | NULL | Vishakhapatnam | | Sharukh | Sheik | 25 | M | 8300 | 103 | 103 | [email protected] | NULL | Pune | | Sarmista | Sharma | 26 | F | 10000 | 104 | 104 | [email protected] | NULL | Mumbai | +------------+--------------+------+------+--------+---------+-----+--------------------+-------+----------------+ 4 rows in set (0.00 sec)
使用 python 的 MYSQL 连接
以下示例从上述两个表中检索数据,通过 EMPLOYEE 表的 contact 列和 CONTACT 表的 ID 列进行组合。
import mysql.connector #establishing the connection conn = mysql.connector.connect( user='root', password='password', host='127.0.0.1', database='mydb' ) #Creating a cursor object using the cursor() method cursor = conn.cursor() #Retrieving single row sql = '''SELECT * from EMPLOYEE INNER JOIN CONTACT ON EMPLOYEE.CONTACT = CONTACT.ID''' #Executing the query cursor.execute(sql) #Fetching 1st row from the table result = cursor.fetchall(); print(result) #Closing the connection conn.close()
输出
[('Krishna', 'Sharma', 26, 'M', 2000, 101, 101, '[email protected]', 9848022338, 'Hyderabad'), ('Raj', 'Kandukuri', 20, 'M', 7000, 102, 102, '[email protected]', 9848022339, 'Vishakhapatnam'), ('Ramya', 'Ramapriya', 29, 'F', 5000, 103, 103, '[email protected]', 9848022337, 'Pune'), ('Mac', 'Mohan', 26, 'M', 2000, 104, 104, '[email protected]', 9848022330, 'Mumbai')]
广告