Home Database Mysql Tutorial mysql需要注意的几个sql语句_MySQL

mysql需要注意的几个sql语句_MySQL

Jun 01, 2016 pm 02:00 PM
sql statement

第一部分 : 文字


1, 从一个表向另一个同样结构的表插入批量的数据

    insert into table1 (select * from table2 )

2, 使用sql语句插入大量语句可以如下方式进行插入.一次提交即可.
    insert into table1 values
    (1,'a'),

    (2,'b'),

    (3,'a'),

    (4,'b'),

    ...............

   3, mysql 取得下一个自动增长的id .取得这个数据是连接独立的.也就是mysql会自动维护没一个链接应该拿到的最大id.

    也就是说,有两个链接同时插入进去这个表各一条记录,则mysql会自动返回相同的最后id.

SELECT LAST_INSERT_ID() from createuserid limit 0,1

4, 返回第一个非空的字符串.如果字段columnname字段中的数据为null,则会返回aaa值.

     COALESCE(columnname,'aaa')

    此方法相当与sqlserver中的 isnull(columnname,'default') 返回第一个非空的字符串.

   5, 修复表.如果mysql的表出现灰,不能被读取,并且表的linux用户组也都正确的情况下.使用如下命令修复表.

    repair table tablename ;

6, 如果字段中的时间为long类型的毫秒数. 使用select FROM_UNIXTIME(875996580) 将之转换为日期类型. 需要注意:

    长度超过一定位数后,需要截取前面的几位才可以.否则不能正确转换.

7, 日期函数 adddate . 参数说明

    函数是对日期进行计算的. 第一个参数是字段,第二个参数是间隔的意思,为关键字.

    第三个参数是要累加多少的,后面的参数是累加的单位

    select adddate(regdate,interval 0 day) from tablename desc limit 0,100

   8 关于子select语句.一定要注意子语句的对记录范围的选取.

    例如:

    select * from table1 where id in (select id from table2)

   如果要添加条件,一定要在子句中加入where

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to create tables with sql server using sql statement How to create tables with sql server using sql statement Apr 09, 2025 pm 03:48 PM

How to create tables using SQL statements in SQL Server: Open SQL Server Management Studio and connect to the database server. Select the database to create the table. Enter the CREATE TABLE statement to specify the table name, column name, data type, and constraints. Click the Execute button to create the table.

How to write a tutorial on how to connect three tables in SQL statements How to write a tutorial on how to connect three tables in SQL statements Apr 09, 2025 pm 02:03 PM

This article introduces a detailed tutorial on joining three tables using SQL statements to guide readers step by step how to effectively correlate data in different tables. With examples and detailed syntax explanations, this article will help you master the joining techniques of tables in SQL, so that you can efficiently retrieve associated information from the database.

How to create Mysql database using phpMyadmin How to create Mysql database using phpMyadmin Apr 10, 2025 pm 10:48 PM

phpMyAdmin can be used to create databases in PHP projects. The specific steps are as follows: Log in to phpMyAdmin and click the "New" button. Enter the name of the database you want to create, and note that it complies with the MySQL naming rules. Set character sets, such as UTF-8, to avoid garbled problems.

How to judge SQL injection How to judge SQL injection Apr 09, 2025 pm 04:18 PM

Methods to judge SQL injection include: detecting suspicious input, viewing original SQL statements, using detection tools, viewing database logs, and performing penetration testing. After the injection is detected, take measures to patch vulnerabilities, verify patches, monitor regularly, and improve developer awareness.

How to check SQL statements How to check SQL statements Apr 09, 2025 pm 04:36 PM

The methods to check SQL statements are: Syntax checking: Use the SQL editor or IDE. Logical check: Verify table name, column name, condition, and data type. Performance Check: Use EXPLAIN or ANALYZE to check indexes and optimize queries. Other checks: Check variables, permissions, and test queries.

How to add columns in PostgreSQL? How to add columns in PostgreSQL? Apr 09, 2025 pm 12:36 PM

PostgreSQL The method to add columns is to use the ALTER TABLE command and consider the following details: Data type: Select the type that is suitable for the new column to store data, such as INT or VARCHAR. Default: Specify the default value of the new column through the DEFAULT keyword, avoiding the value of NULL. Constraints: Add NOT NULL, UNIQUE, or CHECK constraints as needed. Concurrent operations: Use transactions or other concurrency control mechanisms to handle lock conflicts when adding columns.

How to use SQL statement insert How to use SQL statement insert Apr 09, 2025 pm 06:15 PM

The SQL INSERT statement is used to insert data into a table. The steps include: specify the target table to list the columns to be inserted. Specify the value to be inserted (the order of values ​​must correspond to the column name)

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

See all articles