Re: [CPyUG] 问一个关于 flask-sqlalchemy 查询的问题

65 views
Skip to first unread message

yegle

unread,
Sep 18, 2014, 1:07:53 PM9/18/14
to pyth...@googlegroups.com
其他不知道,select XXX from table1, table2, table3这样写,你们的技术主管看到你就死定了

先改成join,咱们再聊怎么用sqlalchemy查询。

2014-09-18 8:23 GMT-07:00 tinytub <zhaope...@gmail.com>:
>
> 写了一个 sql,同时查询多个表的内容...然后, 不知道该怎么翻译成 flash-sqlalchemy了
> select a.map, b.map, b.name , c.map, c.name from test_table1 as a,
> test_table2 as b, test_table3 as c where a.test1_id=b.id and
> a.test2_id=c.id;
>
> 查了半天 模仿sqlalchemy 用db.session.query(test_table1, test_table2,
> test_table3).with_entities(****).with_entities(***) 这样的查询形式在
> flask-sqlalchemy 里试了一下,出的结果只有最后一个 with_entities 所涉及的字段.
>
> 我知道肯定是我使用的方法不对.
>
> 希望各位能给我一个思路,多谢~
>
> --
> --
> 邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
> 规则: http://code.google.com/p/cpyug/wiki/PythonCn
> 发言: pyth...@googlegroups.com
> 详情: http://code.google.com/p/cpyug/wiki/CpyUg
> G+: https://plus.google.com/u/0/communities/108786798869709602787
> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
> ---
> 您收到此邮件是因为您订阅了Google网上论坛中的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com
> 要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/python-cn/a1da6d26-c55f-4cd1-a741-f151d3506d23%40googlegroups.com
> 要查看更多选项,请访问https://groups.google.com/d/optout



--
yegle
http://about.me/yegle

zhaopeng

unread,
Sep 18, 2014, 8:17:02 PM9/18/14
to pyth...@googlegroups.com
那待会到公司我改一下再来提问,木哈哈哈

yegle <cny...@gmail.com>于2014年9月19日星期五写道:
您收到此邮件是因为您订阅了 Google 网上论坛的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com
要在网络上查看此讨论,请访问 https://groups.google.com/d/msgid/python-cn/CAFL5w3VBCzCtC%3DPjFPC2eAV0nnZ-ASGTDv%2Bzyg_Cb27u-qx3yw%40mail.gmail.com
要查看更多选项,请访问 https://groups.google.com/d/optout

Zephyr

unread,
Sep 18, 2014, 11:13:53 PM9/18/14
to pyth...@googlegroups.com

在 2014年9月18日 下午11:23,tinytub <zhaope...@gmail.com>写道:
写了一个 sql,同时查询多个表的内容...然后, 不知道该怎么翻译成 flash-sqlalchemy了
select a.map,  b.map, b.name , c.map, c.name from test_table1 as a, test_table2 as b, test_table3 as c where a.test1_id=b.id and a.test2_id=c.id;

查了半天 模仿sqlalchemy 用db.session.query(test_table1, test_table2, test_table3).with_entities(****).with_entities(***) 这样的查询形式在 flask-sqlalchemy 里试了一下,出的结果只有最后一个 with_entities 所涉及的字段.

我知道肯定是我使用的方法不对.

希望各位能给我一个思路,多谢~


只单纯说 SQLAlchemy 的话,ORM 形式下:

session.query(A.map, B.map, B.name, C.map, C.name).filter(A.test1_id == B.id, A.test2_id == C.id).all()

应该没问题吧。


--
进出自由才是游戏者的生存之道。

http://zouyesheng.com

tinytub

unread,
Sep 18, 2014, 11:16:13 PM9/18/14
to pyth...@googlegroups.com
select a.map,  b.map, b.name , c.map, c.name from test_table1 as a left join (test_table2 as b, test_table3 as c)  on ( a.test1_id=b.id and a.test2_id=c.id; )

恩,这样吧.

在 2014年9月19日星期五UTC+8上午1时07分53秒,@yegle写道:
其他不知道,select XXX from table1, table2, table3这样写,你们的技术主管看到你就死定了

先改成join,咱们再聊怎么用sqlalchemy查询。

2014-09-18 8:23 GMT-07:00 tinytub <zhaope...@gmail.com>:
>
> 写了一个 sql,同时查询多个表的内容...然后, 不知道该怎么翻译成 flash-sqlalchemy了
> select a.map,  b.map, b.name , c.map, c.name from test_table1 as a,
> test_table2 as b, test_table3 as c where a.test1_id=b.id and
> a.test2_id=c.id;
>
> 查了半天 模仿sqlalchemy 用db.session.query(test_table1, test_table2,
> test_table3).with_entities(****).with_entities(***) 这样的查询形式在
> flask-sqlalchemy 里试了一下,出的结果只有最后一个 with_entities 所涉及的字段.
>
> 我知道肯定是我使用的方法不对.
>
> 希望各位能给我一个思路,多谢~
>
> --
> --
> 邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
> 规则: http://code.google.com/p/cpyug/wiki/PythonCn
> 发言: pyth...@googlegroups.com
> 详情: http://code.google.com/p/cpyug/wiki/CpyUg
> G+: https://plus.google.com/u/0/communities/108786798869709602787
> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
> ---
> 您收到此邮件是因为您订阅了Google网上论坛中的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+unsubscribe@googlegroups.com

tinytub

unread,
Sep 18, 2014, 11:19:04 PM9/18/14
to pyth...@googlegroups.com
我丢到 flask-sqlalchemy 下试试...

顺便问一个问题.

用 flask 框架的时候,操作数据库,是直接用 flask-sqlalchemy好呢还是 sqlalchemy?
感觉 flask-sqlalchemy 包装后的对于我这种刚入门的挺友好的.

在 2014年9月19日星期五UTC+8上午11时13分53秒,YS.Zou写道:

谭业微

unread,
Sep 18, 2014, 11:22:10 PM9/18/14
to pyth...@googlegroups.com
都入flask了,肯定flask-sqlalchemy了, 再说,这俩东西是通用的。

--
--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
发言: pyth...@googlegroups.com
详情: http://code.google.com/p/cpyug/wiki/CpyUg
G+: https://plus.google.com/u/0/communities/108786798869709602787
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛中的“python-cn(华蟒用户组,CPyUG 邮件列表)”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/python-cn/9821e9ae-e892-496a-871d-bd4095ff2e0b%40googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

tinytub

unread,
Sep 18, 2014, 11:25:06 PM9/18/14
to pyth...@googlegroups.com
搞定...果然可行,木哈哈哈哈,多谢.
看来是我之前搜索的方法不对...该改改习惯认真看文档了...

在 2014年9月19日星期五UTC+8上午11时13分53秒,YS.Zou写道:
Reply all
Reply to author
Forward
0 new messages