Home > Database > Mysql Tutorial > body text

Usage of @ in mysql

下次还敢
Release: 2024-04-26 04:54:18
Original
492 people have browsed it

In MySQL, the main uses of the @ symbol are: 1. Declare and obtain user variables; 2. Obtain the value of system variables; 3. Declaration and transfer of stored procedure parameters; 4. Check query cache hits; 5 . Create a temporary table.

Usage of @ in mysql

Usage of @ symbol in MySQL

In MySQL, the @ symbol has the following usages:

1. User variables

@ symbols can be used to declare and obtain user variables. The syntax is:

<code>SET @variable_name = value;</code>
Copy after login

For example:

<code>SET @total_sales = (SELECT SUM(sales) FROM orders);</code>
Copy after login

2. System variables

@ symbols can also be used to obtain the values ​​of system variables. The syntax is:

<code>SELECT @@system_variable_name;</code>
Copy after login

For example:

<code>SELECT @@version;</code>
Copy after login

3. Stored procedure parameters

In stored procedures, the @ symbol is used to declare and pass parameters. The syntax is:

<code>CREATE PROCEDURE procedure_name (
    IN @param_name1 data_type,
    IN @param_name2 data_type,
    ...
);</code>
Copy after login

For example:

<code>CREATE PROCEDURE GetCustomerOrders (
    IN @customer_id INT
);</code>
Copy after login

4. Query cache hit

@ symbol can be used to check whether the query hits the query cache. The syntax is:

<code>SELECT /*!@SQL_CACHE*/ * FROM table_name;</code>
Copy after login

If the query hits the cache, the value of @SQL_CACHE is 1, otherwise it is 0.

5. Create a temporary table

@ symbol can be used to create a temporary table. The syntax is:

<code>CREATE TEMPORARY TABLE table_name (
    column_name1 data_type,
    column_name2 data_type,
    ...
)
;</code>
Copy after login

For example:

<code>CREATE TEMPORARY TABLE OrderSummary (
    order_id INT,
    product_id INT,
    quantity INT
)
;</code>
Copy after login

The above is the detailed content of Usage of @ in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!