如何写一个SQL查询来获取这个table中所有对应一个名字的唯一ID?
How to write an SQL query to get all the unique IDs in this table corresponding to a name?
Sl No
ID
Name
1
145928
Jake
2
458921
Abel
3
987468
Jake
4
145928
Drake
5
897426
Jake
6
448961
Jake
您好,我正在尝试获取此 table 中与姓名 Jake 对应的所有 ID。我想收集这些数字并将其存储为 python 列表。请告诉我如何使用 mysql-connect 和 python.
实现此目的
import mysql.connector
# Database helper class to connect, to disconnect and to make queries
class Database:
def __init__(self, host, user, password, db):
self.host = host
self.user = user
self.password = password
self.db = db
self.connection = None
self.cursor = None
def connect(self):
self.connection = mysql.connector.connect(
host=self.host,
user=self.user,
password=self.password,
database=self.db
)
self.cursor = self.connection.cursor()
def disconnect(self):
self.cursor.close()
self.connection.close()
def query(self, query):
self.cursor.execute(query)
return self.cursor.fetchall()
# Connect to database
db = Database('localhost', 'user', 'password', 'database')
db.connect()
# Query database
result = db.query('SELECT * FROM users WHERE name = "Jake"')
for row in result:
# Make something with the result
print(dict(row))
# Disconnect from database
db.disconnect()
Sl No | ID | Name |
---|---|---|
1 | 145928 | Jake |
2 | 458921 | Abel |
3 | 987468 | Jake |
4 | 145928 | Drake |
5 | 897426 | Jake |
6 | 448961 | Jake |
您好,我正在尝试获取此 table 中与姓名 Jake 对应的所有 ID。我想收集这些数字并将其存储为 python 列表。请告诉我如何使用 mysql-connect 和 python.
实现此目的import mysql.connector
# Database helper class to connect, to disconnect and to make queries
class Database:
def __init__(self, host, user, password, db):
self.host = host
self.user = user
self.password = password
self.db = db
self.connection = None
self.cursor = None
def connect(self):
self.connection = mysql.connector.connect(
host=self.host,
user=self.user,
password=self.password,
database=self.db
)
self.cursor = self.connection.cursor()
def disconnect(self):
self.cursor.close()
self.connection.close()
def query(self, query):
self.cursor.execute(query)
return self.cursor.fetchall()
# Connect to database
db = Database('localhost', 'user', 'password', 'database')
db.connect()
# Query database
result = db.query('SELECT * FROM users WHERE name = "Jake"')
for row in result:
# Make something with the result
print(dict(row))
# Disconnect from database
db.disconnect()