Get all results using WHERE clause in MySQL?



To get all results using WHERE clause, you can use LIKE operator. We can also get all the results using “Select * from table_name”.

Let us first create a table:

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name varchar(20)
);
Query OK, 0 rows affected (0.56 sec)

Following is the query to insert records in the table using insert command:

mysql> insert into DemoTable(Name) values('Larry');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable(Name) values('John');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable(Name) values('Bob');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable(Name) values('Carol');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(Name) values('Chris');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(Name) values('David');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(Name) values('Mike');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable(Name) values('Sam');
Query OK, 1 row affected (0.11 sec)

Following is the query to display records from the table using select command:

mysql> select *from DemoTable;

This will produce the following output:

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Larry |
| 2  | John  |
| 3  | Bob   |
| 4  | Carol |
| 5  | Chris |
| 6 | David  |
| 7 | Mike   |
| 8 | Sam    | 
+----+-------+
8 rows in set (0.00 sec)

Query to get all results using WHERE clause:

mysql> select *from DemoTable where Name like '%';

This will produce the following output:

+----+-------+
| Id | Name  |
+----+-------+
|  1 | Larry |
|  2 | John  |
|  3 | Bob   |
|  4 | Carol |
|  5 | Chris |
|  6 | David |
|  7 | Mike  |
|  8 | Sam   |
+----+-------+
8 rows in set (0.00 sec)

Advertisements