Practical SQL tips for 13 mysql Databases

Source: Internet
Author: User
Www.gbtags.comgb4242351.htm MYSQL, as one of the most successful open-source relational databases, has a large number of fans (I am also). In this article, we carefully collected 10 most useful mysql Query Techniques, I hope you will be pleasantly surprised. If you have a very good SQL, please leave a message to share with us! SQL, MYSQ

Http://www.gbtags.com/gb/share/2351.htm MYSQL, as one of the most successful open source relational databases, has a large number of fans (I am also), in this article, we carefully collected 10 most practical mysql Query Techniques, I hope you will be pleasantly surprised. If you have a very good SQL, please leave a message to share with us! SQL, MYSQ


Http://www.gbtags.com/gb/share/2351.htm


As one of the most successful open-source relational databases, MYSQL has a large number of fans (I am also a fan). In this article, we carefully collected 10 most practical mysql Query Techniques, I hope you will be pleasantly surprised. If you have a very good SQL, please leave a message to share with us!

SQL, MYSQL

Use CASE to redefine the Value Type

  1. SELECT id,title,
  2. (CASE date WHEN '0000-00-00' THEN '' ELSE date END) AS date
  3. FROM your_table
  4. SELECT id,title,
  5. (CASE status WHEN 0 THEN 'open' WHEN 1 THEN 'close' ELSE 'standby'END) AS status
  6. FROM your_table

Search for repeated email record Fields

  1. SELECT email, COUNT(email) AS q
  2. FROM emails_table GROUP BY email HAVING q >1
  3. ORDER BY q DESC

Obtain records from random order

  1. SELECT * FROM your_table ORDER BY RAND()

Use update to replace characters in a specified field

  1. UPDATE your_table
  2. SET name=REPLACE(name,'gbin1.com','gbtags.com')
  3. WHERE name LIKE '%John%';

Reset the auto-increment value in the specified table

  1. ALTER TABLE your_table AUTO_INCREMENT =2

The next time you insert data, the id is automatically increased to 2

Add an automatically added field column to the select statement.

  1. set@N=0;
  2. SELECT @N:=@N+1 AS number, name, surname FROM gbtags_users;

Use concat to connect Fields

  1. SELECT CONCAT(name,' ',surname) AS complete_name FROM users

Use the date method to retrieve the specified part of the date type

  1. SELECT id,title, YEAR(date_field) FROM your_table
  2. SELECT id,title,
  3. CONCAT(MONTH(date_field),'/',YEAR(date_field))as new_date
  4. FROM your_table

In the preceding statement, the first select selects the year, and the second selects the combination of the month and the year.

Insert statements for fields of unique key (unique key) Type

  1. INSERT IGNORE INTO tags (tag) VALUES ('good');

This insert statement can be executed multiple times, and no error is reported. Duplicate Inserts will be ignored.

Use full-text index and match search

  1. select*from articles where MATCH(content_column) AGAINST ('music')

You must first add the full-text search index to the specified column (content_column ). Note: If you already have data in the table, no index will be created. Therefore, you need to use an empty table to execute this statement.

If the query is performed one month ago

  1. SELECT user, count(*) AS logins
  2. FROM stat_log
  3. WHERE action='LOGIN' AND dt_when >= DATE_ADD(CURDATE(), INTERVAL -1 MONTH)
  4. GROUP BY user

This statement allows you to query all records of the field dt_when a month ago.

Set the correct Character Set

  1. SET NAMES 'utf8';

Run this statement after the connection starts.

Insert another table from one table

  1. INSERT INTO yourtable (field1,field2,field3)
  2. SELECT newfield1,newfield2,'fixed value'
  3. FROM yourtable2

This statement can help you quickly add specific content from a table to another table.

We hope that the SQL statements we will introduce here will help you better and faster use of Mysql. If you have a lot of self-saved SQL statements, please leave us a message!

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.