3

Hy guys I have this table: VisitedCity

id timestamp name city
1 2022-02-02 10:02:23 Mark LA
2 2022-01-15 08:45:01 Phil NY
3 2022-02-05 11:09:45 John MI

My query :

SELECT * FROM VisitedCity
WHERE timestamp = '2022-02-02'

But it doesn't return any rows. How Could I do it?

6
  • use WHERE timestamp LIKE '2022-02-02%' Commented Feb 21, 2022 at 9:43
  • Review date functions dev.mysql.com/doc/refman/8.0/en/… Commented Feb 21, 2022 at 9:46
  • @Pawan.Java thanks, it works Commented Feb 21, 2022 at 9:46
  • @Pawan.Java This can do for a quick and dirty query, but using string manipulation functions to deal with dates almost never pays off in the long term. Code becomes harder to maintain, performance suffers and it's easy for bugs to slip in. Commented Feb 21, 2022 at 10:09
  • @ÁlvaroGonzález I know but given the problem statement, he was looking for something quick itself. Commented Feb 21, 2022 at 11:34

1 Answer 1

4

You can't use = if you don't want an exact match. You need to query for an interval:

SELECT *
FROM VisitedCity
WHERE timestamp >= '2022-02-02 00:00:00' AND timestamp < '2022-02-03 00:00:00'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I have modified only the last part in timestamp < DATE_ADD( '2022-02-02 ', INTERVAL 1 DAY)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.