Skills of sql query

Essential understanding of sql group by 

 

It can be found that group by is the first line of the result of order by! 

Filter query for fields other than this table

 Query the results of table A, but the filter conditions are the filter conditions of table B, where the primary key of table A is the foreign key of table B, and the table structure is as follows:

Table A:

CREATE TABLE `A` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

Table B:

CREATE TABLE `B` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `address` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL COMMENT 'A表的主键',
  PRIMARY KEY (`id`)
)

A: B: 

 

 The inner join query is:

 

 Among them, exists means: the id existing in A appears at least once in user_id in B, and the application scenario is:

Query table A, but the filter condition has the fields of table B, such as: query users living in Chengdu, Sichuan:

SELECT	* FROM	A 
WHERE
	EXISTS (SELECT * 	FROM	B WHERE	A.id = B.user_id and  B.address="四川成都")

 

 Knowledge point 1:

Mysql is not case-sensitive by default, which means that when table names, column names or strings are used, case will not affect the query results, but if you need case-sensitive in MySQL, you can specify the character set when creating the table as A case-sensitive character set, such asutf8_bin,如下:

 Knowledge point 2:

Is there any difference between join and inner join in mysql? There is no difference, the memory can be omitted to write inner in mysql

 

Guess you like

Origin blog.csdn.net/u013372493/article/details/125747212