

The following statementĭetermines the highest sum of the arms and legs columns in the limbs table and assigns it to the variable: mysql> SELECT := MAX(arms+legs) FROM limbs Īnother use for a variable is to save the result from LAST_INSERT_ID() after creating a new

Statement assigns a column value to a variable, but variables also canīe assigned values from arbitrary expressions. Mysql> DELETE FROM orders WHERE cust_id = DELETE FROM customers WHERE cust_id = preceding SELECT Statements: mysql> SELECT := cust_id FROM customers WHERE cust_id=' customer name ' One way toĭo this is to first save the ID value in a variable, and then refer to If you have aĬustomer name and you want to delete the customer record as well asĪll the customer’s orders, you need to determine the proper cust_id value for that customer, and thenĭelete rows from both the customers and orders tables that match the ID. Indicate which customer each order is associated with.

Suppose that you have a customers table with a cust_id column that identifies each You need to issue successive statements on multiple tables that are The variable can be used in subsequent statements wherever anĮxpression is allowed, such as in a WHERE clause or in an INSERT statement.Ī common situation in which user variables come in handy is when The syntax for assigning a value to a user To save a result returned from one statement, and then refer to it User-defined variable, and then refer to the variable later in your You can assign a value returned by a SELECT statement to a
