Several SQL statements for MySQL Learning

Source: Internet
Author: User

I want to learn several SQL statements that need to be paid attention to when using mysql. It is helpful for beginners:

1. Insert batch data from one table to another table with the same structure

Insertintotable1 (select * fromtable2)

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

Insertintotable1values
(1, 'A '),

(2, 'B '),

(3, 'A '),

(4, 'B '),

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

3. mysql gets the next auto-increasing id, and 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 final id.

SELECTLAST_INSERT_ID () fromcreateuseridlimit0, 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:

Repairtabletablename;

6. If the time in the field is the number of milliseconds of the long type, use selectFROM_UNIXTIME (875996580) to convert it to the date type. Note:

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

7. Description of the adddate parameter of the date function

The function calculates the date. The first parameter is the field, and the second parameter is the meaning of the interval, which is a keyword.

The third parameter is the sum of values, and the subsequent parameter is the sum unit.

Selectadddate (regdate, interval0day) fromtablenamedesclimit0, 100

8. For the sub-select statement, you must select the record range of the sub-statement.

Example: select * fromtable1whereidin (selectidfromtable2)

If you want to add conditions, you must add the where clause.

Related Article

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.