如何使用Python跨 MySQL 表的列执行算术?
Python是一种动态语言, Python应用程序可以与数据库服务器集成。用于从Python访问 MySQL 数据库的模块是 MySQL Connector Python。 PyMySQL、MySQLDB 和 mysqlclient 是其他Python模块,用于在Python中与 MySQL 数据库服务器进行通信。但是,我们将在本文中使用 MySQL Connector Python ,因为它是一个完全用Python编写的 API,这意味着它没有依赖项,只需要标准库。
为了对列数据执行算术运算,MySQL 具有算术运算符。这些运算符可用于对数据执行计算。 MySQL中的算术运算符如下:
Operation | MySQL Arithmetic Operator | Syntax in MySQL |
Addition | + | SELECT op1 + op2 FROM tablename; |
Subtraction | – | SELECT op1 – op2 FROM tablename; |
Division | / | SELECT op1 / op2 FROM tablename; |
Multiplication | * | SELECT op1 * op2 FROM tablename; |
Modulus | % | SELECT op1 % op2 FROM tablename; |
在上表中, op1和op2可以是列名或数值(在这种情况下不需要“from”子句)。以下程序将帮助您更好地了解这些运算符的使用。
使用中的数据库:
我们将在我们的程序中使用产品信息表。它由产品名称、成本价、售价、税金和购买数量组成。
示例 1:加法运算符的使用
Python3
# Import required packages
import mysql.connector
# Establish connection to MySQL database
mydb = mysql.connector.connect(
host = "localhost",
user = "username",
password = "geeksforgeeks",
database = "grocery"
)
# MySQLCursorDict creates a cursor
# that returns rows as dictionaries
mycursor = mydb.cursor( dictionary = True )
# MySQL query for getting total
# sale amount (i.e. selling price + tax)
query = "SELECT Selling_price, \
tax, \
concat(Selling_price + tax) AS sale_amount \
FROM product"
# Execute the query
mycursor.execute( query )
# Fetch result of query
myresult = mycursor.fetchall()
# Print result of query
print(f"SP \t Tax \t Sale Amount")
for row in myresult:
# Each value printed for display purpose (you can simply print row)
print(f"{ row[ 'Selling_price' ]} \t { row[ 'tax' ]} \t { row[ 'sale_amount' ]}")
mydb.close()
Python3
# Import required packages
import mysql.connector
# Establish connection to MySQL database
mydb = mysql.connector.connect(
host = "localhost" ,
user = "username" ,
password = "geeksforgeeks" ,
database = "grocery"
)
# MySQLCursorDict creates a cursor
# that returns rows as dictionaries
mycursor = mydb.cursor(dictionary = True )
# MySQL query for getting profit
# (i.e. selling price - cost price)
query = "SELECT selling_price, \
cost_price, \
concat(selling_price - cost_price) AS profit \
FROM product_info"
# Execute the query
mycursor.execute( query )
# Fetch result of query
myresult = mycursor.fetchall()
# Print result of query
print(f"SP \t CP \t Profit")
for row in myresult:
print(f"{ row[ 'selling_price' ]} \t { row[ 'cost_price' ]} \t { row[ 'profit' ]}")
mydb.close()
Python3
# Import required packages
import mysql.connector
# Establish connection to MySQL database
mydb = mysql.connector.connect(
host = "localhost" ,
user = "username" ,
password = "geeksforgeeks" ,
database = "grocery"
)
# MySQLCursorDict creates a cursor that returns rows as dictionaries
mycursor = mydb.cursor(dictionary = True)
# MySQL query for getting total amount (i.e. sale amount * quantity)
query = "SELECT selling_price, \
tax, \
concat((selling_price * tax)) AS total_amount \
FROM product_info"
# Execute the query
mycursor.execute( query )
# Fetch result of query
myresult = mycursor.fetchall()
# Print result of query
print(f"SP \t Tax \t Total")
for row in myresult:
print(f"{row[ 'selling_price' ]} \t { row[ 'tax' ]}\t{row['total_amount']}")
# Close database connection
mydb.close()
Python3
# Import required packages
import mysql.connector
# Establish connection to MySQL database
mydb = mysql.connector.connect(
host = "localhost" ,
user = "username" ,
password = "geeksforgeeks" ,
database = "grocery"
)
# MySQLCursorDict creates a cursor
# that returns rows as dictionaries
mycursor = mydb.cursor( dictionary = True )
# MySQL query for getting halved selling price of all products
query = "SELECT selling_price, \
concat(selling_price / 2) AS discount_price \
FROM product_info"
# Execute the query
mycursor.execute( query )
# Fetch result of query
myresult = mycursor.fetchall()
# Print result of query
print(f"SP \t Discounted Price")
for row in myresult:
print(f"{ row[ 'selling_price' ]} \t { row ['discount_price']}")
# Close database connection
mydb.close()
Python3
# Import required packages
import mysql.connector
# Establish connection to MySQL database
mydb = mysql.connector.connect(
host = "localhost" ,
user = "username" ,
password = "geeksforgeeks" ,
database = "grocery"
)
# MySQLCursorDict creates a cursor
# that returns rows as dictionaries
mycursor = mydb.cursor(dictionary = True)
# MySQL query for getting remainder
query = "SELECT selling_price, \
cost_price, \
concat(selling_price % cost_price) AS mod_example \
FROM product_info"
# Execute the query
mycursor.execute( query )
# Fetch result of query
myresult = mycursor.fetchall()
# Print result of query
print(f"SP \t Qnty \t MOD")
for row in myresult:
print(f"{ row [ 'selling_price' ]}
\t { row [ 'cost_price' ]}
\t{row[ 'mod_example' ]}")
# Close database connection
mydb.close()
输出:
示例 2:减法运算符的使用。
蟒蛇3
# Import required packages
import mysql.connector
# Establish connection to MySQL database
mydb = mysql.connector.connect(
host = "localhost" ,
user = "username" ,
password = "geeksforgeeks" ,
database = "grocery"
)
# MySQLCursorDict creates a cursor
# that returns rows as dictionaries
mycursor = mydb.cursor(dictionary = True )
# MySQL query for getting profit
# (i.e. selling price - cost price)
query = "SELECT selling_price, \
cost_price, \
concat(selling_price - cost_price) AS profit \
FROM product_info"
# Execute the query
mycursor.execute( query )
# Fetch result of query
myresult = mycursor.fetchall()
# Print result of query
print(f"SP \t CP \t Profit")
for row in myresult:
print(f"{ row[ 'selling_price' ]} \t { row[ 'cost_price' ]} \t { row[ 'profit' ]}")
mydb.close()
输出:
示例 3:乘法运算符的使用
蟒蛇3
# Import required packages
import mysql.connector
# Establish connection to MySQL database
mydb = mysql.connector.connect(
host = "localhost" ,
user = "username" ,
password = "geeksforgeeks" ,
database = "grocery"
)
# MySQLCursorDict creates a cursor that returns rows as dictionaries
mycursor = mydb.cursor(dictionary = True)
# MySQL query for getting total amount (i.e. sale amount * quantity)
query = "SELECT selling_price, \
tax, \
concat((selling_price * tax)) AS total_amount \
FROM product_info"
# Execute the query
mycursor.execute( query )
# Fetch result of query
myresult = mycursor.fetchall()
# Print result of query
print(f"SP \t Tax \t Total")
for row in myresult:
print(f"{row[ 'selling_price' ]} \t { row[ 'tax' ]}\t{row['total_amount']}")
# Close database connection
mydb.close()
输出:
示例 4:除法运算符的使用。
蟒蛇3
# Import required packages
import mysql.connector
# Establish connection to MySQL database
mydb = mysql.connector.connect(
host = "localhost" ,
user = "username" ,
password = "geeksforgeeks" ,
database = "grocery"
)
# MySQLCursorDict creates a cursor
# that returns rows as dictionaries
mycursor = mydb.cursor( dictionary = True )
# MySQL query for getting halved selling price of all products
query = "SELECT selling_price, \
concat(selling_price / 2) AS discount_price \
FROM product_info"
# Execute the query
mycursor.execute( query )
# Fetch result of query
myresult = mycursor.fetchall()
# Print result of query
print(f"SP \t Discounted Price")
for row in myresult:
print(f"{ row[ 'selling_price' ]} \t { row ['discount_price']}")
# Close database connection
mydb.close()
输出:
示例 5:模数运算符的使用。
蟒蛇3
# Import required packages
import mysql.connector
# Establish connection to MySQL database
mydb = mysql.connector.connect(
host = "localhost" ,
user = "username" ,
password = "geeksforgeeks" ,
database = "grocery"
)
# MySQLCursorDict creates a cursor
# that returns rows as dictionaries
mycursor = mydb.cursor(dictionary = True)
# MySQL query for getting remainder
query = "SELECT selling_price, \
cost_price, \
concat(selling_price % cost_price) AS mod_example \
FROM product_info"
# Execute the query
mycursor.execute( query )
# Fetch result of query
myresult = mycursor.fetchall()
# Print result of query
print(f"SP \t Qnty \t MOD")
for row in myresult:
print(f"{ row [ 'selling_price' ]}
\t { row [ 'cost_price' ]}
\t{row[ 'mod_example' ]}")
# Close database connection
mydb.close()
输出: