简体   繁体   English

Python MySql 从空字符串中选择

[英]Python MySql selecting from empty string

I'm trying to select from a table so that the parameter I input can be nothing but it still will select everything我正在尝试从表中获取 select 以便我输入的参数可以是任何东西,但它仍然会 select 一切

name = ""
mycursor.execute('SELECT * FROM table WHERE name LIKE "%%%s%%"' (name, ))

I was hoping to have if name is something like "", then everything in the table will be fetched.我希望如果名称类似于“”,那么表中的所有内容都将被获取。

With LIKE it will not be possible, however you can achieve it using REGEXP使用 LIKE 是不可能的,但是您可以使用 REGEXP 实现它

name = "John Doe"
regexp='^.*$'
value = (regexp if name=="" else name)
query =  "SELECT * FROM mysql.user WHERE name REGEXP '{}' ;".format(value)
print(query)

Note: However be wary that if you are looking for a single user, and if there are other user names that matches the string Like "John Doe1" it will return both the entries注意:但是请注意,如果您正在寻找单个用户,并且如果有其他用户名与字符串匹配,例如“John Doe1”,它将返回两个条目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM