B
B
bear14892021-07-08 18:09:37
MySQL
bear1489, 2021-07-08 18:09:37

How to select data from MySql table by minimum date with additional condition?

Help to make a SQL query for a beginner. There is a table accounts consisting of three fields (login, status, date). The status field can have multiple values, such as "active" and "free"

I need to return the string with the smallest date and whose status will be free.

This is how it turns out to select only by the minimum date: Select * from `accounts` where date IN (SELECT MIN(`date`) FROM `accounts`)

This is how I tried to write an additional condition: Select * from `accounts` where date IN (SELECT MIN(`date`) FROM `accounts`) AND `status`= 'free'

I went to learn SQL, I will be very grateful for the help kind people)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-07-08
@bear1489

So, well, I'm not a real DBA, but I see you have such an error:
you check the date field, and it can be like this: 2016-09-26 14:31:31
I would do this

select * from accounts where date(date) IN (select date(min(date)) from accounts) and status = 'free';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question