T
T
TonyJa2018-05-11 08:39:34
MySQL
TonyJa, 2018-05-11 08:39:34

How to return a list from MySQL?

I have a list of purchases in the database, the "purchase" entity has a "Date" field.
Can you please tell me how to get a list of purchases for a given date range.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Gornostaev, 2018-05-11
@TonyJa

Query query = session.createQuery("from Purchase p where p.bought between :startDate and :endDate");
query.setParameter("startDate", someDate);
query.setParameter("endDate", anotherDate);
List<Purchase> purchases = query.list();

C
Che_Bu_Rashka, 2018-05-11
@Che_Bu_Rashka

Using any ORM you will get a list for a Select request
Example with ApacheCayenne
list roles;
SelectQuery select_role = new SelectQuery(Role.class);
roles = Context.getContext().performQuery(select_role);
as a result in roles there will be a list
In Hibernate I think in the same way. However, dates are a little more complicated.
This is how the Cayenne proposes to work with dates

Calendar c = new GregorianCalendar();
c.set(c.get(Calendar.YEAR) - 100, 0, 1, 0, 0, 0);
Expression qualifier3 = Expression.fromString("artist.dateOfBirth < $date");
qualifier3 = qualifier3.expWithParameters(Collections.singletonMap("date", c.getTime()));
SelectQuery select3 = new SelectQuery(Painting.class, qualifier3);
List paintings3 = context.performQuery(select3);
There is a selection of paintings by those artists who were born more than 100 years ago

D
Dmitry Kim, 2018-05-11
@kimono

SELECT * FROM `buys` WHERE `date` BETWEEN '2000-01-01' AND '2000-01-31'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question