Some common SQL statements in mysql

Source: Internet
Author: User
This article describes some common SQL statements in mysql, including instances with DISTINCTSELECTDELETETRUNCATEINSERTINTO.

This article describes some common SQL statements in mysql, including instances with DISTINCT SELECT DELETE TRUNCATE INSERT.


1. query the different parts of table A and table B.
Field not in (result set)

SELECT aurl FROM atab WHERE aurl not in (select distinct burl FROM btab)
Note: mysql supports subqueries from 5.0.

If you compare multiple fields, you can use concat to concatenate the fields into strings for comparison. For example:

SELECT aurl, aclass FROM atab where concat (aurl, aclass) not in (select distinct concat (burl, bclass) FROM btab)

2. Clear the content in table.
Delete from atab

Truncate table atab


To DELETE some records in a table, you can only use the DELETE statement.

Delete from atab WHERE aid> 1

If DELETE does not add a WHERE clause, it is the same as the truncate table, but they are a little different. DELETE can return the number of deleted records, while truncate table returns 0.

If an auto-increment field exists in a TABLE, after deleting all records using truncate table and DELETE without WHERE clause, the auto-increment field restores the starting value to 1. if you do not want to do this, you can add the permanent WHERE statement in the DELETE statement, such as WHERE 1 or WHERE true.

Delete from atabl WHERE 1

3. Add a specified column of a table to another table.
Insert into atab (aurl) SELECT burl FROM btab

Insert burl in the btab table to the aurl column in The atab table

Here we will introduce some basic-level statements. If you need them, refer to explain.

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.