django原始查询百分号问题 [英] django raw query percent sign problem

查看:455
本文介绍了django原始查询百分号问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试Django在中的原始sql查询,如函数,但结果为空。我尝试mysql客户端工具这个查询并获得很多记录。如何解决这个问题?

I trying to Django raw sql query in like function, but results is empty. I try mysql client tool this query and get many records. how to solution this problem?

我的查询:

  SELECT s.* , s.id as pk 
    FROM d_status as s, 
         (select post_id 
            from p_useractions 
           where from_user_id in 
                 (select to_user_id 
                    from p_fallowers 
                   where from_user_id = 1)
         ) as a 
   WHERE s.from_user_id = 1 OR 
         s.text like '%@Mustafa Yontar2123%' OR 
         s.id = a.post_id 
GROUP BY s.id 
ORDER BY s.last_update 
   LIMIT 25


推荐答案

MySQL


With LIKE you can use the following two wildcard characters in the pattern.

    Character  Description
    %          Matches any number of characters, even zero characters
    _          Matches exactly one character

To test for literal instances of a wildcard character, precede it by the 
escape character. If you do not specify the ESCAPE character, \ is assumed.

所以你的模式需要是'\%@ Mustafa Yontar2123\%'。查询的Python代码将如下所示:

So your pattern needs to be '\%@Mustafa Yontar2123\%'. The Python code for the query will look like this:

query = r"""SELECT s.* , s.id as pk FROM d_status as s, 
                   (SELECT post_id FROM p_useractions WHERE from_user_id in
                     (SELECT to_user_id FROM p_fallowers WHERE from_user_id = 1)) as a
            WHERE s.from_user_id = 1 or
                  s.text like '\%@Mustafa Yontar2123\%' or
                  s.id = a.post_id
            GROUP BY s.id
            ORDER BY s.last_update
            LIMIT 25"""

PostgreSQL SQLite 提到同样的事情。

这篇关于django原始查询百分号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆