SQL decision statement usage and multiple table query _mssql

Source: Internet
Author: User

1. Format time SQL statements

In this case, I did a random two tables, and the actual is not very consistent, just want to explain the wording of the SQL statement.

Example 1 is in table format as follows:

Requirements: Query out this table, but need to make time field format for YYYY-MM-DD, such as: 2013-08-13

SQL notation:

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

Run Result:

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

Example two or three table structure is as follows:

Requirements: Query out the main table, requiring that username and secondname be displayed in the main table

SQL notation:

SELECT u.id,n. ' Name ', u.timetype,u.time,s. ' Name ' from ' User ' U, ' name ' N, ' second '
WHERE u.userid=n.id and U.secondi D=s.id

Run Result:

 3.when then judgment statement

Example three table structure is as follows:

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

SQL notation:

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

Run Result:

The above is a small set to introduce the SQL Judgment statement usage and multiple table query, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.