当前位置:网站首页>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边栏推荐
- [combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
- Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
- Suffix derivation based on query object fields
- Recent learning experience
- Unity2018 to wechat games without pictures
- After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
- Caddy server agent
- Summary and Reflection on the third week of winter vacation
- How about the Moco model?
- Max of PHP FPM_ Some misunderstandings of children
猜你喜欢

SQL injection -day16

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

English语法_形容词/副词3级 - 倍数表达

FBI 警告:有人利用 AI 换脸冒充他人身份进行远程面试

Web3 credential network project galaxy is better than nym?

How to analyze the rising and falling rules of London gold trend chart

Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing

Analysis of the reasons why enterprises build their own software development teams to use software manpower outsourcing services at the same time
![[Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University](/img/52/b97c618a8f2eb29ad0ccca221bb5c1.jpg)
[Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University

Install apache+php+mysql+phpmyadmin xampp and its error resolution
随机推荐
Grammaire anglaise Nom - Classification
Know what it is, and know why, JS object creation and inheritance [summary and sorting]
Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing
Use of unsafe class
Naoqi robot summary 27
FBI 警告:有人利用 AI 换脸冒充他人身份进行远程面试
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
MySQL duplicate check
English grammar_ Noun classification
What London Silver Trading software supports multiple languages
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
G1 garbage collector of garbage collector
Implementation of cqrs architecture mode under Kratos microservice framework
How to disable the clear button of ie10 insert text box- How can I disable the clear button that IE10 inserts into textboxes?
2022-2028 global sepsis treatment drug industry research and trend analysis report
2022-2028 global solid phase extraction column industry research and trend analysis report
2022-2028 global plasmid DNA cdmo industry research and trend analysis report
2022-2028 global marking ink industry research and trend analysis report
English语法_名词 - 分类
论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding