
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display substrings of different length with a single MySQL query and combine the result
Let us first create a table −
mysql> create table DemoTable856(Title text); Query OK, 0 rows affected (0.89 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable856 values('Introduction to MySQL'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable856 values('Java in depth'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable856 values('C++ with Data Structure'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable856;
This will produce the following output −
+-------------------------+ | Title | +-------------------------+ | Introduction to MySQL | | Java in depth | | C++ with Data Structure | +-------------------------+ 3 rows in set (0.00 sec)
Following is the query to display the substrings of different length and combine the result using UNION ALL −
mysql> select substr(Title,1,12) from DemoTable856 UNION ALL select substr(Title,1,4) from DemoTable856;
This will produce the following output −
+--------------------+ | substr(Title,1,12) | +--------------------+ | Introduction | | Java in dept | | C++ with Dat | | Intr | | Java | | C++ | +--------------------+ 6 rows in set (0.03 sec)
- Related Questions & Answers
- MySQL query to get the length of all columns and display the result in a single new column?
- Count multiple rows and display the result in different columns (and a single row) with MySQL
- Combine two MySQL fields and update a third one with result?
- Count values based on conditions and display the result in different columns with MySQL?
- MySQL query to combine two columns in a single column?
- Combine columns before matching it with LIKE in a single query in MySQL?
- A single MySQL query to combine strings from many rows into a single row and display the corresponding User Id sum in another column?
- How can I display MySQL query result vertically?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Combine SUM and FORMAT in MySQL to format the result
- Counting different distinct items in a single MySQL query?
- Set different IDs for records with conditions using a single MySQL query
- Count and sort rows with a single MySQL query
- MySQL query to count the duplicate ID values and display the result in a separate column
- Select distinct names from two columns in MySQL and display the result in a single column
Advertisements