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!