- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get only a single value from a specific MySQL row?
For this, use SELECT INTO variable with where clause. Let us first create a table −
mysql> create table DemoTable1896 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(20), StudentMarks int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1896(StudentName,StudentMarks) values('Chris',56); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1896(StudentName,StudentMarks) values('David',98); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1896(StudentName,StudentMarks) values('Mike',89); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1896(StudentName,StudentMarks) values('Sam',78); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1896;
This will produce the following output −
+-----------+-------------+--------------+ | StudentId | StudentName | StudentMarks | +-----------+-------------+--------------+ | 1 | Chris | 56 | | 2 | David | 98 | | 3 | Mike | 89 | | 4 | Sam | 78 | +-----------+-------------+--------------+ 4 rows in set (0.00 sec)
Here is the query to get value from a specific MySQL row −
mysql> set @Name:=NULL; Query OK, 0 rows affected (0.00 sec) mysql> select StudentName into @Name from DemoTable1896 where StudentMarks=98; Query OK, 1 row affected (0.00 sec)
Now you can display the value of the above variable −
mysql> select @Name;
This will produce the following output −
+-------+ | @Name | +-------+ | David | +-------+ 1 row in set (0.00 sec)
- Related Articles
- Return only a single row from duplicate rows with MySQL
- MySQL query to get a specific row from rows
- MySQL query to get the highest value from a single row with multiple columns
- Replace only a specific value from a column in MySQL
- MySQL select only a single value from 5 similar values?
- Delete all rows except only a specific row in MySQL?
- Updating only a single column value in MySQL?
- Update only a single column value in MySQL
- Get the sum only from specific cells in MySQL?
- MySQL insert a value to specific row and column
- Get the Average of Average in a single MySQL row?
- How can we delete a single row from a MySQL table?
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- How to exclude a specific row from a table in MySQL?
- MySQL query to get a single value from position of comma-separated string?

Advertisements