MySQL SQL syntax summary

Source: Internet
Author: User

MySQL PRIMARY key operation

To delete a table primary key:

ALTER TABLE student drop PRIMARY key;

Add Table primary key:

ALTER TABLE student Add primary key (ID);

Reset the primary key increment (if you need to perform a delete table primary KEY statement first):

ALTER TABLE student Modify ID bigint (a) Auto_increment primary key;

Verify that the name is duplicate

Plus limit 1 is telling MySQL to find an instant return without wasting the time behind

Select COUNT (*) from student where name = ' Tom ' limit 1;

GROUP BY statement: divided by criteria into different groups

New Customer table

Id Name Cost
1 Tom 1000
2 Jojnny 2000
3 Tom 350
4 Bruce 730
5 Mark 3000
6 Ethan 2300
7 Bruce 1740

Query the total cost per customer:

Select name, sum (cost) from the customer group by customer;

The result of the query is:

Name SUM (COST)
Bruce 2470
Ethan 2300
Johnny 2000
Mark 3000
Tom 1350

ORDER BY statement: in conditional order

Search by cost from small to large

SELECT * from the customer order by cost;

Query results are

Id Customer Cost
3 Tom 350
4 Bruce 730
1 Tom 1000
7 Bruce 1740
2 Johnny 2000
6 Ethan 2300
5 Mark 3000

Group BY ORDER by combination: grouped by grouping criteria and sorted by sorting criteria

According to the total cost of customers from small to large arrangement

Select name, sum (cost) from the customer group by customer order by sum (cost);

The query results are:

Name SUM (COST)
Tom 1350
Johnny 2000
Ethan 2300
Bruce 2470
Mark 3000

Distinct statement: Removing duplicates

Find out all the customer names

Select distinct name from customer;

The query results are:

Name
Tom
Johnny
Bruce
Mark
Ethan

MySQL SQL syntax summary

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.