
- 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
How do I pass a variable to a MySQL script?
You can pass a variable to a MySQL script using session variable. First you need to set a session variable using SET command. After that you need to pass that variable to a MySQL script.
The syntax is as follows −
First Step: Use of Set command.
SET @anyVariableName − = ’yourValue’;
Second Step: Pass a variable to a MySQL script.
UPDATE yourTableName SET yourColumnName1 = yourColumnName1+integerValue WHERE yourColumnName2 = @anyVariableName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table Employee_Information -> ( -> EmployeeId int NOT NULL AUTO_INCREMENT, -> EmployeeName varchar(20) NOT NULL, -> EmployeeSalary int, -> EmployeeStatus varchar(20), -> PRIMARY KEY(EmployeeId) -> ); Query OK, 0 rows affected (0.53 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into Employee_Information(EmployeeName,EmployeeSalary,EmployeeStatus) values('Sam',17650,'FullTime'); Query OK, 1 row affected (0.13 sec) mysql> insert into Employee_Information(EmployeeName,EmployeeSalary,EmployeeStatus) values('Carol',12000,'Trainee'); Query OK, 1 row affected (0.18 sec) mysql> insert into Employee_Information(EmployeeName,EmployeeSalary,EmployeeStatus) values('Bob',17650,'FullTime'); Query OK, 1 row affected (0.20 sec) mysql> insert into Employee_Information(EmployeeName,EmployeeSalary,EmployeeStatus) values('Mike',12000,'Trainee'); Query OK, 1 row affected (0.14 sec) mysql> insert into Employee_Information(EmployeeName,EmployeeSalary,EmployeeStatus) values('John',17650,'FullTime'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement.
mysql> select *from Employee_Information;
The following is the output −
+------------+--------------+----------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | EmployeeStatus | +------------+--------------+----------------+----------------+ | 1 | Sam | 17650 | FullTime | | 2 | Carol | 12000 | Trainee | | 3 | Bob | 17650 | FullTime | | 4 | Mike | 12000 | Trainee | | 5 | John | 17650 | FullTime | +------------+--------------+----------------+----------------+ 5 rows in set (0.00 sec)
The following is the query to pass a variable to a MySQL script −
mysql> set @EmpStatus − = 'FullTime'; Query OK, 0 rows affected (0.03 sec) mysql> update Employee_Information set EmployeeSalary = EmployeeSalary+6500 where EmployeeStatus = @EmpStatus; Query OK, 3 rows affected (0.18 sec) Rows matched − 3 Changed − 3 Warnings − 0
Now check the table records once again using SELECT statement. I have incremented the EmployeeSalary with 6500, for the employees working FullTime.
The query is as follows −
mysql> select *from Employee_Information;
The following is the output −
+------------+--------------+----------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | EmployeeStatus | +------------+--------------+----------------+----------------+ | 1 | Sam | 24150 | FullTime | | 2 | Carol | 12000 | Trainee | | 3 | Bob | 24150 | FullTime | | 4 | Mike | 12000 | Trainee | | 5 | John | 24150 | FullTime | +------------+--------------+----------------+----------------+ 5 rows in set (0.00 sec)
- Related Articles
- How do I add a simple jQuery script to WordPress?
- How do I make an executable from a Python script?
- How do I pass an event handler to a component in ReactJS?
- How do I assign a dictionary value to a variable in Python?
- How to pass a variable to an exception in Python?
- How to pass a variable into a jQuery attribute-contains selector?
- How do I check if a Python variable exists?
- What is a Test Script and how do I write one?
- How do I pass environment variables to Docker containers?
- How to pass a variable from Activity to Fragment in Android?
- How to pass a date variable in sql query in a JSP?
- How do I declare a global variable in Python class?
- How do I avoid the variable value in a MySQL stored procedure to change when records are updated?
- How to Pass Command Line Arguments to Bash Script
- How do I remove a MySQL database?
