SQL Judgment statement usage and multiple table queries

Source: Internet
Author: User

1. Format time SQL statements

In this example, I casually made two tables, and the actual is not very consistent, just want to explain the wording of the SQL statement.

Example 1 table format is as follows:

  

Requirements: Query out the table, but you need to make the time field in the format of YYYY-MM-DD, for example: 2013-08-13

SQL notation:

SELECT U.id,u.userid,u.timetype,date_format (Time, '%y-%m-%d ') as Time,secondid from ' User ' u

Operation Result:

  

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

Example two or three table structure is as follows:

  

  

  

Requirements: Querying out the main table, requiring the display of username and secondname in the main table

SQL notation:

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

Operation Result:

  

3.when then judgment statement

The three-table structure is as follows:

  

Requirements: Query the table above, if the TimeType field value is 2 o'clock, display the second day of the month, if the TimeType field value is 4 o'clock, display the fourth day of the month

SQL notation:

SELECT U.id,u.userid, (case is u.timetype=2 then ' the next day of the month ' when u.timetype=4 and then ' fourth day of the Month ' END) as TimeType, U.time,u.second Id from ' User ' u

Operation Result:

  

SQL Judgment statement usage and multiple table queries

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.