如何使用SQL炼金术执行“以开始”查询?

2024-06-09 08:54:58 发布

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

我正在学习使用SQL炼金术连接到mysql数据库。我想从数据库中提取以给定字符串开头的记录。我知道为了简单的平等我需要做的就是

queryRes = ses.query(Table).filter(Tablee.fullFilePath == filePath).all()
result = []

我该怎么做?

queryRes = ses.query(Table).filter(Table.fullFilePath.startsWith(filePath)).all()
result = []

也许查询会是这样的?

q = ses.query(Table).filter(Table.fullFilePath.like('path%')).all()

Tags: 字符串数据库sql记录mysqltableresultall
3条回答

如果需要不区分大小写的比较,请使用^{}

session.query(SomeTable).filter(SomeTable.some_column.ilike('bla%')).all()

SQLAlchemy有一个^{} column property,所以它的工作原理与您想象的完全一样:

queryRes = ses.query(Table).filter(Table.fullFilePath.startswith(filePath)).all()

这是纯SQL:

SELECT * FROM table WHERE field LIKE "string%"

SQL炼金术是:

q = ses.query(Table).filter(Table.fullFilePath.like('path%')).all()

相关问题 更多 >