SQL查询我想进入SQLAlchemy的形式。但我不能写查询与sqlalchemy框架。
查询:
SELECT * FROM Availability_Schedule
WHERE Schedule_ID NOT IN (Select Schedule_ID FROM Appointments WHERE Status = 1)
AND Staff_ID = 1
SQL模型:
class AvailabilitySchedule(db.Model):
__tablename__ = 'Availability_Schedule'
Schedule_ID = Column(INTEGER, primary_key=True)
Schedule_Date = Column(Date)
Slot_ID = Column(ForeignKey('Time_Slots.Slot_ID', ondelete='RESTRICT'), nullable=False, index=True)
Staff_ID = Column(ForeignKey('Hospital_Staff.Staff_ID'), nullable=False, index=True)
Room_ID = Column(ForeignKey('Rooms.Room_ID'), index=True)
Room = relationship('Room')
Time_Slot = relationship('TimeSlot')
Hospital_Staff = relationship('HospitalStaff')
class Appointment(db.Model):
__tablename__ = 'Appointments'
Appointment_ID = Column(INTEGER, primary_key=True, autoincrement=True)
Schedule_ID = Column(ForeignKey('Availability_Schedule.Schedule_ID'), index=True)
Status = Column(Integer)
Patient_ID = Column(ForeignKey('Patients.Patient_ID'), index=True)
Type = Column(String(255))
Patient = relationship('Patient')
Availability_Schedule = relationship('AvailabilitySchedule')
SQL炼金术:
schedules = AvailabilitySchedule.query.filter_by(
Staff_ID = doctorID,
Schedule_ID,not_in(Appointment.query.filter_by(Status = 1).all())
).all()
1条答案
按热度按时间xzabzqsa1#
这是你需要的两个技巧。
.in_( ... )
1.使用以下运算符处理求反:
~