Collection of common operations of msyql (function, joint query, etc.) + sql instance

  1. Display the column information of the table, an example is as follows:
    #The table name is t_category_base
    show full COLUMNS from t_category_base
  2.  The conversion of string and time, the example is as follows:
    #Character creation time, accurate to year, month, day, hour, minute and second
    STR_TO_DATE('2016-01-02','%Y-%m-%d %T')
    
    #Time to string, accurate to year, month, day, hour, minute, second, take the createdate attribute (timestamp type) of the query table test as an example
    select DATE_FORMAT(createdate,'%Y-%m-%d %T') as createdate from test where createdate is not null;                                                                            
  3. %Y: represents a 4-digit year
    %y: represents the year of 2
     
    %m: represents month, the format is (01...12)  
    %c: represents month, the format is (1...12)
     
    %d: represents the number of days in the month, the format is (00...31)  
    %e: represents the number of days in the month, the format is (0...31)
     
    %H: represents the hour, the format is (00...23)  
    %k: represents the hour, the format is (0...23)  
    %h: represents the hour, the format is (01...12)  
    %I: represents the hour, the format is (01...12)  
    %l : represents the hour, the format is (1...12)
      
    %i: Represents minutes, the format is (00...59)
     
    %r: represents time, the format is 12 hours (hh:mm:ss [AP]M)  
    %T: represents time, the format is 24 hours (hh:mm:ss)       
  4. After the union query is grouped by id, use GROUP_CONCAT to combine multiple attributes separated by commas to form a new attribute. The example is as follows:
    #One is the student table student with fields id, student_name, school_id, the other is the school table school with two fields id, school_name
    # insert data separately
    insert into student values(1,'tom',1);
    insert into student values(2,'jim',1);
    insert into school values(1,'qinghua school');
    
    #Joint query, use group_concat to combine student names, group by school id
    select sl.school_name,group_concat(st.name) as student_name from student st,school sl where st.school_Id = sl.id group by sl.id                                                
  5. mysql convert string to case
    # Convert to lowercase
    LCASE('string');
    
    # to uppercase
    UCASE('string')
     

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326681849&siteId=291194637