0

How can I make a complex query with ActiveJDBC?

I have a model Student and a model Class

Student:
   id
   first_name
   last_name


Class:
   id
   name
   student_id

I'm given two student ids 1 and 50 for example

I want to get all the classes such that the student id is between 1 and 50 and such that the first name matches "Dan"

I know I can do the following to get the list of classes between student id [1 - 50]:

List<Class> classesList = Class.where("student_id >= ? and student_id <= ?", firstStudent.getId(), SecondStudent.getId());

but then how I do restrict the student name?

I also have requirements to make more complex queries (specially with many-to-many models) but I'm not sure how to get around it with ActiveJDBC

1 Answers1

0

Here is how to restrict to first name "Dan":

List<Class> classesList = Class.where("student_id >= ? and student_id <= ? and first_name = ?", firstStudent.getId(), SecondStudent.getId(), "Dan");

For many-to-many you need to provide more details to get help

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • the student name is only in the Student model and not the Class model though –  Oct 30 '14 at 05:12
  • In that case you need to write a free-hand SQL: `List classesList = Class.findBySql("select * from classes, students where classes.student_id >= ? and classes.student_id <= ? and students.id = classes.student_id and students.first_name = ?", firstStudent.getId(), SecondStudent.getId(), "Dan");` – ipolevoy Oct 30 '14 at 17:16