
- 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
Using variables with MySQL prepare statement
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20), LastName varchar(20) ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(FirstName,LastName) values('John','Smith'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName,LastName) values('David','Miller'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName,LastName) values('John','Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName,LastName) values('Chris','Brown'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-----------+----------+ | Id | FirstName | LastName | +----+-----------+----------+ | 1 | John | Smith | | 2 | David | Miller | | 3 | John | Doe | | 4 | Chris | Brown | +----+-----------+----------+ 4 rows in set (0.00 sec)
Following is the query to set variables while using prepare statement in MySQL −
mysql> set @yourTableName :='DemoTable'; Query OK, 0 rows affected (0.00 sec) mysql> set @query :=CONCAT('select `LastName` from ',@yourTableName, ' where Id=3 '); Query OK, 0 rows affected (0.00 sec) mysql> prepare myStatement from @query; Query OK, 0 rows affected (0.00 sec) Statement prepared mysql> execute myStatement;
This will produce the following output −
+----------+ | LastName | +----------+ | Doe | +----------+ 1 row in set (0.00 sec)
- Related Articles
- Set user-defined variable with table name in MySQL prepare statement?
- Using Update statement with TINYINT in MySQL?
- Display records with conditions set using if statement in UPDATE statement with MySQL
- How to prepare bank reconciliation statement?
- How to set two variables in a stored procedure with a single MySQL select statement?
- Using User-Defined Variables in MySQL
- How to compare two variables in an if statement using Python?
- Display different variables in MySQL using LIKE?
- Using Options to Set MySQL Program Variables
- MySQL SELECT IF statement with OR?
- MySQL CREATE statement with KEY keyword
- MySQL If statement with multiple conditions?
- Using CREATE TABLE AS statement with UNION of two tables in MySQL
- State the methods to prepare a bank reconciliation statement
- MySQL order by field using CASE Statement

Advertisements