通过SQLAlchemy合并共同的朋友

时间:2018-08-19 18:15:57

标签: python sqlalchemy flask-sqlalchemy

me = User.query.filter_by(username=me)

them = User.query.filter_by(username=them)

my_friends = me.friends.all()

their_friends = them.friends.all()

如果my_friends返回['user1', 'user2', 'user3', 'user4']

their_friends返回['user1', 'user2', 'user3']

如何获得两个用户之间的共同朋友?

我可以使用

list(set(my_friends).intersection(their_friends))

但是我宁愿通过SQL来完成。

1 个答案:

答案 0 :(得分:0)

.btn-new:active, .btn-roll:active, .btn-hold:active { margin: 2px 0 -2px 0; }

.btn-style { font-family: 'Montserrat', sans-serif; font-weight: 300; color: #5C5C5C; background: none; outline: none; border: none; margin: 0; } .btn-new { position: absolute; left: 50%; transform: translate(-50%, -50%); top: 30px; font-size: 25px; } .btn-roll { position: absolute; left: 50%; transform: translate(-50%, -50%); top: 370px; font-size: 25px; } .btn-hold { position: absolute; left: 50%; transform: translate(-50%, -50%); top: 415px; font-size: 25px; } button:hover { color: black; } .btn-new:active, .btn-roll:active, .btn-hold:active { margin: 2px 0 -2px 0; }

<button type="button" class="btn-new btn-style">New Game</button> <button type="button" class="btn-roll btn-style">Roll Dice</button> <button type="button" class="btn-hold btn-style">Hold</button>

me = User.query.filter_by(username=me)

them = User.query.filter_by(username=them)

是我所需要的。