SQL空参数值

2024-05-26 16:29:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个查询,它返回我的数据库中单个产品的月销售额之和。查询工作正常

不是为“=?”设置一个值,而是在“=?”中存储一个空值,而不影响查询。例如,如果我输入'Espresso'作为产品,省略milkOptions,输入'Small'作为大小,我是否可以返回所有小Espresso的查询结果。我想要这个特性是因为我想要不同程度的特异性

import sqlite3
product = input("Enter product")
milkOptions = input("Enter milkoptions")
size = input("Enter size")

conn=sqlite3.connect("system.db")
cur=conn.cursor()
test = cur.execute("""
select count(product), strftime("%m-%Y", orderDate) as month 
from customerOrders
where product = ? and milkOptions = ? and size = ? 
group by product, month""",(product, milkOptions, size)).fetchall()

+---------+---------------+--------+-------------+------------+
| orderid |    product    |  size  | milkOptions | orderDate  |
+---------+---------------+--------+-------------+------------+
|       1 | Espresso      | Small  | Soya        | 2019-10-29 |
|       2 | Cappucino     | Small  | SemiSkimmed | 2019-10-29 |
|       3 | Cappucino     | Small  | SemiSkimmed | 2019-10-29 |
|       4 | Cappucino     | Medium | SemiSkimmed | 2019-10-29 |
|       5 | Hot Chocolate | Medium | Coconut     | 2019-10-29 |
|       6 | Hot Chocolate | Medium | Coconut     | 2019-10-29 |
+---------+---------------+--------+-------------+------------+

Tags: inputsize产品productconnsqlite3smallmedium

热门问题