V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
john990
V2EX  ›  SQLAlchemy

SQLAlchemy 如何查询 参数 in 字段?

  •  
  •   john990 · 2015-02-13 10:57:43 +08:00 · 13496 次点击
    这是一个创建于 3331 天前的主题,其中的信息可能已经有所发展或是发生改变。
    数据库中字段类似这样
    TAG_ID(varchar)
    -------------
    10,13,15,20
    3,10,15,20
    3,15

    现给定一个tag=10,查询包含这个tag的数据

    用sql应该这样写:
    SELECT * FROM table WHERE FIND_IN_SET('10',TAG_ID)

    用SQLAlchemy应该怎么操作(Model name = Post)

    mysql数据库
    5 条回复    2015-02-13 11:40:22 +08:00
    timonwong
        2
    timonwong  
       2015-02-13 11:25:03 +08:00   ❤️ 1
    from sqlalchemy.sql.expression import func

    session.query(Post).filter(func.find_in_set('10', Post.c.tag_id))
    john990
        3
    john990  
    OP
       2015-02-13 11:25:19 +08:00
    @awanabe 这个是Django的,SQLAlchemy好像没有这个
    timonwong
        4
    timonwong  
       2015-02-13 11:31:46 +08:00   ❤️ 1
    补充
    from sqlalchemy import func 就可以了,一回事。
    http://docs.sqlalchemy.org/en/rel_0_9/core/sqlelement.html#sqlalchemy.sql.expression.func
    john990
        5
    john990  
    OP
       2015-02-13 11:40:22 +08:00
    @timonwong 谢谢,按你的方法解决了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1544 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 17:01 · PVG 01:01 · LAX 10:01 · JFK 13:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.