在 django raw sql 中使用 like 语句时出错
我有一个原始的 sql 查询,在执行时总是出错。这是我的查询,
Users.objects.raw('select target, username from users where location like \'%s%%\' and date(modified) = \'2011-06-14\'',[location])
我采用 location = 'BUILD'
位置值将为 'BUILD_A'、'BUILD_B'、'BUILD_C'。
当我执行原始 sql 时,以下是我收到的错误。
数据库错误:(1064,“您的 SQL 语法有错误;请检查手册 与您的 MySQL 服务器版本相对应,以便在附近使用正确的语法 'BUILD'%' 和日期(修改)= '2011-06-14'' 在第 1 行”)
在 MySQL 术语中,我需要执行以下查询:
Select target, username from users where location like 'BUILD%' and target = '2011-06-14'
我已经用 google 搜索了它,但无法获取它。请有人帮我
I have a raw sql query which is always giving error while executing. Here is my query
Users.objects.raw('select target, username from users where location like \'%s%%\' and date(modified) = \'2011-06-14\'',[location])
I am taking the location = 'BUILD'
Location values would be 'BUILD_A', 'BUILD_B','BUILD_C'.
When I am executing the raw sql, below is the error I am getting.
DatabaseError: (1064, "You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use near
'BUILD'%' and date(modified) = '2011-06-14'' at line 1")
In MySQL terms I need to execute the following query:
Select target, username from users where location like 'BUILD%' and target = '2011-06-14'
I have googled it but could not able to get it. Please some one help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经用这种方式解决了我的问题。
location = location + '%'
users_list = Users.objects.raw('从位置如 %s 且日期(修改)= %s 的用户中选择目标、用户名',tuple([ location,date]))
上面的语句执行完美,没有任何错误,我也可以在模板中渲染结果。
I have solved my problem in this way.
location = location + '%'
users_list = Users.objects.raw('select target, username from users where location like %s and date(modified) = %s',tuple([location,date]))
The above statement executes perfectly without any error and I can able to render the results in template also.
下面的查询对我有用,
查询=“从位置如‘BUILD%’的用户中选择目标、用户名”
结果 = Users.objects.raw(查询)
Below query works for me,
query = "Select target, username from users where location like 'BUILD%'"
results = Users.objects.raw(query)