MySQL - Data

### data###

Added:
  Insert data: insert into ads (stu_name,sex,money_2) values ​​('lynn','female',100.23);

  Insert multiple data: insert into ads () values ​​(),(),()...... --- ',' separate multiple data values

  Note: When the corresponding field is not written in the statement, all parameters are filled in by default, that is, the number of values ​​must be consistent with all fields in the table


Delete:
  Clear the data in the table: truncate tablename; --- The self-increasing id will start over, which is faster than delete, because it is deleted from the disk and cannot be recovered

  Delete the entire table data: delete from ads; --- The self-increasing id will continue to grow

  Delete the specified data: delete from ads where name = 'lynn';


Change:
  Modify the data of the entire field: update ads set money = 80;

  Modify the data of the specified conditions: update ads set money = 80 where name = 'lynn';

  Modify multi-field data: update ads set sex = 'nv',money = 80 where name = 'lynn';

  Modify on the basis of the original value: update ads set money = money+100;


check:

  ##### Single table####

  Specify the query field: select id,name from ads;

  Query all fields: select * from ads;

  Specify conditional query: select * from ads where sex = 'male' and phone = 110;
  (conditions can be connected with and/or; or in (,); or between 10 and 100)

  Limit the number of queries: select * from ads limit 5; (query the first 5 rows)

  Specify the range of query lines: select * from ads limit 2,4; (query lines 3, 4, and 5; 0: the first line)

  Fuzzy matching - wildcard: select * from ads where addr like '%Tokyo%'; (%: wildcard)

  Table alias: select a.name from ads as a where a.phone = 110; (as can be omitted)

  Aliasing the field: select name as name from ads where phone = 110; (as can be omitted)

  Fuzzy matching - single character: select * from ads where addr like 'dong_'; ('_': single character match)

  Sort query: select * from ads order by id asc; (asc: ascending order; desc: descending order)

  Empty data query: select * from ads a where a.addr = '' or a.addr is null; ('': empty string, null: empty)

  De-duplication query: select distinct money from ads; (distinct: de-duplicate data in a field)

  Statistical query: select count(*) number of students from ads; (count: count of rows)

  Maximum value query: select max(money) the most money from ads; (max: maximum value)

  Minimum value query: select min(money) the least money from ads; (min: minimum value)

  Average query: select avg(money) average money from ads; (avg: average)

  Sum query: select sum(money) how much money from ads; (sum: sum)

  Group: select * from ads group by sex; (group by: group by field)
  Example: According to gender, how many boys and girls have selected
    sex gender, count(*) the number of people from ads group by sex;

  Add conditional query after grouping: select * from ads group by sex having name like 'yao%'; (using having)
  Note: after grouping condition judgment use having

  Multi-field grouping: select * from ads group by sex, name; (principle, fields are first combined, and then grouped)


  #### Multi-table association ####

  '='关联:select * from user a,accounts b where a.id = b.user_id and a.username = 'niuniu';

  'left join on': select * from blk a left join score b on a.id =b.id; (check out all the data in the left table, and find out if there is a match in the right table)

  'right join on': select * from blk a right join score b on a.id=b.id; (check out all the data in the right table, and find out if there is a match in the left table)

  'inner join': select * from blk a inner join score b on a.id=b.id; (check out the matching data in both tables)


  #### Subqueries ####

  1. Use the result of one sql as the condition of another sql
  select * from score a where a.s_id = (select id from blk where stu_name='Axiang');

  2. Use the result of one sql as the table of another sql
  select * from score a,(select id from blk where stu_name='Axiang') b where a.s_id=b.id;


  #### Merge query results####

  Merge two query result sets; Condition: The two query fields have the same number and the same data type, and the combined field name is the same as the previous query field name

  1. select id, stu_name from blk union slect id, t_name from teacher ; (union: can deduplicate)

  2. select id, stu_name from blk union all slect id, t_name from teacher; (union: no duplication; high execution efficiency)

 


Remark:
  Turn off autocommit: set @@autocommit = 0;

  View the submission method: select @@autocommit; (1: automatic submission, 0: manual submission)

  Manual commit: commit;

  Rollback: rollback; (used before commit, that is, under the condition of non-automatic commit, if you accidentally execute the wrong statement, you can roll back as long as the commit is not executed)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325957882&siteId=291194637