-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw3school_pythonmysqljoin.py
More file actions
59 lines (42 loc) · 1.45 KB
/
w3school_pythonmysqljoin.py
File metadata and controls
59 lines (42 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Python MySQL Join
# Join Two or More Tables
# You can combine rows from two or more tables, based on a related column between them, by using a JOIN statement.
import mysql.connector # import MySQL Connector
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password = "mahanta1",
database = "mydatabase"
)
mycursor = mydb.cursor()
# sql1 = "CREATE TABLE products (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, url VARCHAR(255) NOT NULL) "
# sql2 = "CREATE TABLE users (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, fav INT(11) NOT NULL, FOREIGN KEY (fav) REFERENCES products(id)) "
# mycursor.execute(sql1)
# mydb.commit()
# mycursor.execute(sql2)
# mydb.commit()
# sql2 = "CREATE TABLE products (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, url VARCHAR(255) NOT NULL) "
# mydb.commit()
"""
sql = "INSERT INTO users (name,fav) VALUES (%s, %s)"
val = [
('Peter', 1),
('John', 1),
('Amy', 2),
('Hannah',2),
('Michael',3)
]
mycursor.executemany(sql, val)
mydb.commit()
"""
# Example - Join users and products tables: to see the name and users favorite product:
sql = "SELECT \
users.name AS user, \
products.name AS favorite, \
products.url AS url \
FROM users \
INNER JOIN products ON users.fav = products.id"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)