当前位置:网站首页>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边栏推荐
- Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
- PHP determines which constellation it belongs to today
- Transformer T5 model read slowly
- Introduction to SSH Remote execution command
- Multifunctional web file manager filestash
- Opencv learning notes (continuously updated)
- JS_ Array_ sort
- In addition to the prickles that pierce your skin, there are poems and distant places that originally haunt you in plain life
- G1 garbage collector of garbage collector
- Implementation of cqrs architecture mode under Kratos microservice framework
猜你喜欢
SQL custom collation
Recommend a simple browser tab
CTO and programmer were both sentenced for losing control of the crawler
2022-2028 global sepsis treatment drug industry research and trend analysis report
235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
SQL: special update operation
Naoqi robot summary 27
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
Chisel tutorial - 06 Phased summary: implement an FIR filter (chisel implements 4-bit FIR filter and parameterized FIR filter)
随机推荐
MySQL duplicate check
Summary and Reflection on the third week of winter vacation
Torch learning notes (3) -- univariate linear regression model (self training)
Know what it is, and know why, JS object creation and inheritance [summary and sorting]
SQL: special update operation
Transformer T5 model read slowly
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
leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
2022-2028 global petroleum pipe joint industry research and trend analysis report
Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing
[combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
Sensor 调试流程
SQL injection -day16
leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】
leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]
Recommend a simple browser tab
English語法_名詞 - 分類
Niuke monthly race 31 minus integer
php-fpm的max_chindren的一些误区