SQL statement usage and multi-Table query,

Source: Internet
Author: User

SQL statement usage and multi-Table query,

1. SQL statement for formatting time

In this example, I made two tables randomly, which is not very consistent with the actual situation. I just want to explain how to write the SQL statement.

Example 1:

Requirement: This table is queried, but the time format of the time field must be yyyy-MM-dd, for example

SQL statement:

SELECT u.id,u.userId,u.timeType,DATE_FORMAT(time,'%Y-%m-%d') AS time,secondId FROM `user` u

Running result:

 2. Multi-Table query (three-Table query)

For example, the structure of table 2 and table 3 is as follows:

Requirement: the master table is queried. the username and secondname must be displayed in the master table.

SQL statement:

SELECT u.id,n.`name`,u.timeType,u.time,s.`name` FROM `user` u,`name` n,`second` s  WHERE u.userId=n.id AND u.secondId=s.id

Running result:

 3. when then judgment statement

The structure of Table 3 is as follows:

Requirement: query the table above. If the timeType field value is 2, the second day of each month is displayed. If the timeType field value is 4, the fourth day of each month is displayed.

SQL statement:

SELECT u. id, u. userId, (case when u. timeType = 2 then' the second day of each month 'when u. timeType = 4 then' the fourth day of each month 'end) AS timeType, u. time, u. secondId FROM 'user' u

Running result:

The preceding section describes the usage of SQL statements and multi-table queries. I hope this will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.