john990

SQLAlchemy 如何查询 参数 in 字段?

  •  
  •   john990 · Feb 13, 2015 · 14464 views
    This topic created in 4104 days ago, the information mentioned may be changed or developed.
    数据库中字段类似这样
    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 replies    2015-02-13 11:40:22 +08:00
    timonwong
        2
    timonwong  
       Feb 13, 2015   ❤️ 1
    from sqlalchemy.sql.expression import func

    session.query(Post).filter(func.find_in_set('10', Post.c.tag_id))
    john990
        3
    john990  
    OP
       Feb 13, 2015
    @awanabe 这个是Django的,SQLAlchemy好像没有这个
    timonwong
        4
    timonwong  
       Feb 13, 2015   ❤️ 1
    补充
    from sqlalchemy import func 就可以了,一回事。
    http://docs.sqlalchemy.org/en/rel_0_9/core/sqlelement.html#sqlalchemy.sql.expression.func
    john990
        5
    john990  
    OP
       Feb 13, 2015
    @timonwong 谢谢,按你的方法解决了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3127 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 15:01 · PVG 23:01 · LAX 08:01 · JFK 11:01
    ♥ Do have faith in what you're doing.