
- 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
Return similar names from different columns with distinct space allocations in MySQL?
For this, use LTRIM() and RTRIM(), since the names are similar but have different left and right space allocations. For example, “ John “ and “John”.
Let us first create a table −
mysql> create table DemoTable614 (EnterName varchar(100),ReennterName varchar(100)); Query OK, 0 rows affected (0.97 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable614 values(' John ','John'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable614 values('Carol ','Bob'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable614 values('David ','David'); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable614;
This will produce the following output −
+------------+--------------+ | EnterName | ReennterName | +------------+--------------+ | John | John | | Carol | Bob | | David | David | +------------+--------------+ 3 rows in set (0.00 sec)
Following is the query to return similar names from different columns with distinct space allocations −
mysql> select *from DemoTable614 where LTRIM(RTRIM(EnterName)) = LTRIM(RTRIM(ReennterName));
This will produce the following output −
+------------+--------------+ | EnterName | ReennterName | +------------+--------------+ | John | John | | David | David | +------------+--------------+ 2 rows in set (0.00 sec)
- Related Articles
- Get maximum age from records with similar student names in MySQL
- Select distinct names from two columns in MySQL and display the result in a single column
- Select distinct combinations from two columns in MySQL?
- Select distinct values from two columns in MySQL?
- MYSQL select DISTINCT values from two columns?
- MySQL query to calculate the days between two dates from different columns but similar rows
- Concatenate columns from different tables in MySQL
- Select distinct values from three columns and display in a single column with MySQL
- Select Statement to retrieve the same first names with similar last name (but different case) using MySQL?
- How MySQL stored GENERATED COLUMNS are different from MySQL virtual GENERATED COLUMNS?
- MySQL query to sum the values of similar columns from two different tables for a particular ID
- How to select different values from same column and display them in different columns with MySQL?
- Get MySQL maximum value from 3 different columns?
- Return a list from different rows into a single field with MySQL
- MySQL query to copy records from one table to another with different columns

Advertisements