码迷,mamicode.com
首页 > 其他好文 > 详细

peewee

时间:2019-04-23 09:21:48      阅读:1154      评论:0      收藏:0      [点我收藏+]

标签:sel   iter   class   相同   HERE   not   union   ber   monthly   

query = Facility.select()   等价于SQL   SELECT * FROM facilities
Facility.select(Facility.name, Facility.membercost)  等价于  SELECT name, membercost FROM facilities;
Facility.select().where(Facility.membercost > 0) 等价于 SELECT * FROM facilities WHERE membercost > 0
Facility.select().where(Facility.name.contains(‘tennis‘)) SELECT * FROM facilities WHERE name ILIKE ‘%tennis%‘;

 或者   Facility.select().where(Facility.name ** ‘%tennis%‘)

 

query = Facility.select().where(Facility.facid.in_([1, 5]))   in操作

 

SELECT name,
CASE WHEN monthlymaintenance > 100 THEN ‘expensive‘ ELSE ‘cheap‘ END
FROM facilities;

 对应ORM语句

cost = Case(None, [(Facility.monthlymaintenance > 100, ‘expensive‘)], ‘cheap‘)
query = Facility.select(Facility.name, cost.alias(‘cost‘))

 

比较日期

SELECT memid, surname, firstname, joindate FROM members
WHERE joindate >= ‘2012-09-01‘;
Member.select(Member.memid, Member.surname, Member.firstname, Member.joindate).where(Member.joindate >= datetime.date(2012, 9, 1))

 

SELECT DISTINCT surname FROM members ORDER BY surname LIMIT 10;
Member.select(Member.surname).order_by(Member.surname).limit(10).distinct()

 UNION 操作  把查询到的数据 合并 到一起  前提:列数相同

SELECT surname FROM members UNION SELECT name FROM facilities;
lhs = Member.select(Member.surname)
rhs = Facility.select(Facility.name)
query = lhs | rhs
  • |   代表   UNION        相同的只显示一次
  • +   代表   UNION ALL    都显示
  • &   代表   INTERSECT    只显示相同的
  •  代表   EXCEPT       

 

SELECT MAX(join_date) FROM members;
query = Member.select(fn.MAX(Member.joindate))

 

peewee

标签:sel   iter   class   相同   HERE   not   union   ber   monthly   

原文地址:https://www.cnblogs.com/escapist/p/10754197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!