
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can I achieve similar result like using a loop in MySQL WHERE clause to display only alternate ID records?
Get similar results using MySQL IN(). Let us first create a table −
mysql> create table DemoTable ( ClientId int, ClientName varchar(100), ClientAge int ); Query OK, 0 rows affected (0.70 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'Chris',34); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(101,'Robert',31); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(103,'David',33); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(104,'Mike',45); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(105,'Sam',39); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ | 100 | Chris | 34 | | 101 | Robert | 31 | | 103 | David | 33 | | 104 | Mike | 45 | | 105 | Sam | 39 | +----------+------------+-----------+ 5 rows in set (0.00 sec)
Following is the query to get similar results like a loop using MySQL IN() −
mysql> select ClientId,ClientName,ClientAge from DemoTable where ClientId IN(101,103,105);
This will produce the following output −
+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ | 101 | Robert | 31 | | 103 | David | 33 | | 105 | Sam | 39 | +----------+------------+-----------+ 3 rows in set (0.00 sec)
- Related Articles
- How can I display MySQL query result vertically?
- Can we use the result of a SUM() function in MySQL WHERE clause
- How can I use SPACE() function with MySQL WHERE clause?
- Fetch similar ID records from two tables in MySQL
- MySql how to display the records with latest ID in a table?
- Use MySQL LIKE and NOT LIKE to display similar results?
- How Can MySQL GROUP BY clause behave like DISTINCT clause?
- How to filter data using where Clause and “LIKE” in Android sqlite?
- MySQL automatic string to integer casting in WHERE clause to fetch a specific id
- Using LIKE clause twice in a MySQL query
- Only display row with highest ID in MySQL
- MySQL query to display records from a table filtered using LIKE with multiple words?
- How can I get the records from MySQL table in result set in a particular way?
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?
- Exclude some ID records from a list and display rest in MySQL

Advertisements