john990

SQLAlchemy 如何查询 参数 in 字段?

  •  
  •   john990 · Feb 13, 2015 · 14973 views
    This topic created in 4289 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
    timonwong
    Feb 13, 2015❤️1
    2
    from sqlalchemy.sql.expression import func

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