当前位置:网站首页>Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
2022-07-03 18:44:00 【fyin1314】
problem :
I've just recently started using SQLAlchemy and am still having trouble wrapping my head around some of the concepts. I just started using SQLAlchemy, But I still can't understand some concepts .
Boiled down to the essential elements, I have two tables like this (this is through Flask-SQLAlchemy): Boil down to basic elements , I have two such watches ( This is through Flask-SQLAlchemy):
class User(db.Model): __tablename__ = 'users' user_id = db.Column(db.Integer, primary_key=True)class Posts(db.Model): __tablename__ = 'posts' post_id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.user_id')) post_time = db.Column(db.DateTime) user = db.relationship('User', backref='posts')How would I go about querying for a list of users and their newest post (excluding users with no posts). How will I query the user list and its latest posts ( Users without posts are not included ).If I was using SQL, I would do: If I use SQL, I will do this :
SELECT [whatever]FROM posts AS p LEFT JOIN users AS u ON u.user_id = p.user_idWHERE p.post_time = (SELECT MAX(post_time) FROM posts WHERE user_id = u.user_id)So I know exactly the "desired" SQL to get the effect I want, but no idea how to express it "properly" in SQLAlchemy. So I know exactly “ Desired ”SQL To get the effect I want , But I don't know how to SQLAlchemy in “ Correctly ” Express it .
Edit: in case it's important, I'm on SQLAlchemy 0.6.6. edit : In case of a one thousand , I'm using SQLAlchemy 0.6.6.
Solution :
Reference resources : https://stackoom.com/en/question/Q2cS边栏推荐
- FBI 警告:有人利用 AI 换脸冒充他人身份进行远程面试
- webcodecs
- Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals
- 4. Load balancing and dynamic static separation
- leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】
- Day-27 database
- Computer graduation project PHP library book borrowing management system
- Torch learning notes (4) -- torch's dynamic calculation diagram
- 2022-2028 global petroleum pipe joint industry research and trend analysis report
- Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
猜你喜欢

G1 garbage collector of garbage collector

Unity webgl optimization

In addition to the prickles that pierce your skin, there are poems and distant places that originally haunt you in plain life

Raft 日志复制

2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report

Mysql45 lecture learning notes (II)

Su embedded training - Day10

What London Silver Trading software supports multiple languages

Computer graduation design PHP makeup sales Beauty shopping mall

2022-2028 global petroleum pipe joint industry research and trend analysis report
随机推荐
235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
多媒体NFT聚合平台OKALEIDO即将上线,全新的NFT时代或将来临
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
The vscode code is automatically modified to a compliance code when it is formatted and saved
042. (2.11) do it when it's time to do it
After nohup NPM start &, close the shell window directly, and the process closes accordingly
Setinterval CPU intensive- Is setInterval CPU intensive?
041. (2.10) talk about manpower outsourcing
2022-2028 global aircraft head up display (HUD) industry research and trend analysis report
Ping problem between virtual machine and development board
shell 脚本中关于用户输入参数的处理
Enterprise custom form engine solution (12) -- form rule engine 2
Zhengda futures news: soaring oil prices may continue to push up global inflation
2022-2028 global scar care product industry research and trend analysis report
189. Rotation array
Boost. Asio Library
Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
MySQL duplicate check
Torch learning notes (2) -- 11 common operation modes of tensor
Reappearance of ASPP (atlas spatial pyramid pooling) code