Several "SQL" statements worth attention in MySQL _ MySQL

Source: Internet
Author: User
Several "SQL" statements worth attention in MySQL ◆ 1. Insert batch data from one table to another table with the same structure

Insert into table1 (select * from table2)

◆ 2. Insert a large number of SQL statements as follows.

Insert into table1 values

(1, 'A '),

(2, 'B '),

(3, 'A '),

(4, 'B '),

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

◆ 3. mysql gets the id of the next automatic growth. the data obtained is connected independently. that is, mysql automatically maintains the maximum id that should be obtained by no link. that is to say, if two links are inserted into each record of the table at the same time, mysql will automatically return the same last id.

SELECT LAST_INSERT_ID () from createuserid limit 0, 1

◆ 4. return the first non-empty string. if the data in the columnname field is null, the aaa value is returned.

COALESCE (columnname, 'AAA ')

This method is equivalent to isnull (columnname, 'default') in sqlserver and returns the first non-null string.

◆ 5. repair the table. if the mysql table is grayed out and cannot be read, and the linux User Group of the table is correct, run the following command to repair the table.

Repair table tablename;

◆ 6. if the time in the field is the number of milliseconds of the long type, use select FROM_UNIXTIME (875996580) to convert it to the date type. note:

After the length exceeds a certain number of digits, the first few digits must be intercepted. Otherwise, the conversion fails.

◆ 7. date function adddate.

Parameter description

The function calculates the date. the first parameter is a field, and the second parameter is a keyword. the third parameter is the value to be accumulated, and the subsequent parameter is the unit of accumulation.

Select adddate (regdate, interval 0 day)

From tablename desc limit 0,100

◆ 8. for subselect statements, be sure to select the record range of substatements.

For example:

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

To add conditions, you must add where to the clause.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.