
- 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
MySQL query to select multiple rows effectively?
You need to use index to select multiple rows effectively. Let us first create a table −
mysql> create table DemoTable1501 -> ( -> Id int NOT NULL PRIMARY KEY, -> URL text -> ); Query OK, 0 rows affected (0.62 sec)
Here is the query to create index −
mysql> create index id_index on DemoTable1501(Id); Query OK, 0 rows affected (0.23 sec) Records: 0 Duplicates: 0 Warnings: 0
Insert some records in the table using insert command −
mysql> insert into DemoTable1501 values(101,'www.facebook.com'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1501 values(110,'www.google.com'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1501 values(220,'www.gmail.com'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable1501 values(350,'www.youtube.com'); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1501;
This will produce the following output −
+-----+------------------+ | Id | URL | +-----+------------------+ | 101 | www.facebook.com | | 110 | www.google.com | | 220 | www.gmail.com | | 350 | www.youtube.com | +-----+------------------+ 4 rows in set (0.00 sec)
Following is the query to select multiple rows efficiently −
mysql> select * from DemoTable1501 -> where Id in(101,220,350);
This will produce the following output −
+-----+------------------+ | Id | URL | +-----+------------------+ | 101 | www.facebook.com | | 220 | www.gmail.com | | 350 | www.youtube.com | +-----+------------------+ 3 rows in set (0.00 sec)
To prove this, use the SHOW command in which the Handler_read_key uses 3 out of 4 Ids −
mysql> SHOW STATUS LIKE 'Handler_%';
This will produce the following output −
+----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | Handler_commit | 1 | | Handler_delete | 0 | | Handler_discover | 0 | | Handler_external_lock | 2 | | Handler_mrr_init | 0 | | Handler_prepare | 0 | | Handler_read_first | 0 | | Handler_read_key | 3 | | Handler_read_last | 0 | | Handler_read_next | 0 | | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 0 | | Handler_rollback | 0 | | Handler_savepoint | 0 | | Handler_savepoint_rollback | 0 | | Handler_update | 0 | | Handler_write | 0 | +----------------------------+-------+ 18 rows in set (0.00 sec)
- Related Articles
- MySQL query to select too many rows?
- MySQL query to select top n rows efficiently?
- MySQL select query with multiple WHERE?
- MySQL query to check if multiple rows exist?
- MySQL query to count rows in multiple tables
- MySQL query to select rows older than a week?
- How to insert multiple rows with single MySQL query?
- MySQL query to get result from multiple select statements?
- Insert multiple rows in a single MySQL query
- MySQL query to select rows one batch at a time
- How to get multiple rows in a single MySQL query?
- How to obtain multiple rows in a single MySQL query?
- MySQL query to select rows except first row in descending order?
- MySQL select query to select rows from a table that are not in another table?
- How to order results of a query randomly & select random rows in MySQL?

Advertisements