Syntax error How to select from table where conditions are set for id and name in MySQL?

How to select from table where conditions are set for id and name in MySQL?



Let us first create a table −

mysql> create table DemoTable819(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(100)
);
Query OK, 0 rows affected (0.88 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable819(StudentName) values('Chris');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable819(StudentName) values('Robert');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable819(StudentName) values('Adam');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable819(StudentName) values('Mike');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable819(StudentName) values('Sam');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable819;

This will produce the following output −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|         1 | Chris       |
|         2 | Robert      |
|         3 | Adam        |
|         4 | Mike        |
|         5 | Sam         |
+-----------+-------------+
5 rows in set (0.00 sec)

Following is the query to select from the table where conditions are set for name and id −

mysql> select *from DemoTable819 where StudentName='Robert' and StudentId > 1;

This will produce the following output −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|         2 | Robert      |
+-----------+-------------+
1 row in set (0.00 sec)
Updated on: 2019-09-03T11:44:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements