Search records on the basis of date in MySQL?

MySQLMySQLi Database

Let us first create a table -

mysql> create table DemoTable732 (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   PassengerId int,
   PassengerName varchar(100),
   PassengerAge int,
   PassengerTravelDatetime datetime
);
Query OK, 0 rows affected (0.67 sec)

Insert some records in the table using insert command -

mysql> insert into DemoTable732(PassengerId,PassengerName,PassengerAge,PassengerTravelDatetime) values(110,'Chris',25,'2019-07-23 12:45:56');
Query OK, 1 row affected (0.40 sec)
mysql> insert into DemoTable732(PassengerId,PassengerName,PassengerAge,PassengerTravelDatetime) values(120,'Robert',24,'2019-07-21 11:05:00');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable732(PassengerId,PassengerName,PassengerAge,PassengerTravelDatetime) values(120,'Sam',26,'2019-07-21 11:05:00');
Query OK, 1 row affected (0.30 sec)

Display all records from the table using select statement -

mysql> select *from DemoTable732;

This will produce the following output -

+----+-------------+---------------+--------------+-------------------------+
| Id | PassengerId | PassengerName | PassengerAge | PassengerTravelDatetime |
+----+-------------+---------------+--------------+-------------------------+
| 1  | 110         | Chris         | 25           | 2019-07-23 12:45:56     |
| 2  | 120         | Robert        | 24           | 2019-07-21 11:05:00     |
| 3  | 120         | Sam           | 26           | 2019-07-21 11:05:00     |
+----+-------------+---------------+--------------+-------------------------+
3 rows in set (0.00 sec)

Here is the query to simple SELECT COUNT SQL query error -

mysql> select count(Id) from DemoTable732 where PassengerId=120 and
date(PassengerTravelDatetime)='2019-07-21';

This will produce the following output -

+-----------+
| count(Id) |
+-----------+
| 2         |
+-----------+
1 row in set (0.00 sec)
raja
Published on 22-Aug-2019 11:15:58
Advertisements