SQL Advanced: What happened to the select statement, consolidating the foundation for writing complex SQL

1. Preparations

Two tables are prepared for testing

-- ----------------------------
-- Table structure for info
-- ----------------------------
DROP TABLE IF EXISTS `info`;
CREATE TABLE `info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT NULL,
  `views` int(255) DEFAULT NULL,
  `info_type_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

-- ----------------------------
-- Table structure for info_type
-- ----------------------------
DROP TABLE IF EXISTS `info_type`;
CREATE TABLE `info_type` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

Import the above sql into navicat

info sheet

info_type table

2. What happened to the select statement?

Now I want to query all the data in the info table, it is easy to write the following sql

SELECT * FROM info;

I don't know if you have thought about it, what happened when you selected?

In fact, when the sql statement is executed, if you see select, it will query one by one according to the table name behind from, and finally merge it into the result set.

If we add a where condition, then screening will be performed.

Note that this screening occurs on every statement.

For example, when you execute the SELECT * from info where info_type_id = 2; statement, it takes 0.001ms to query this data

Excuse me, does he meet the where conditions? If it does not match, then this one will be filtered out and continue to the next one.

In the end, those that do not meet the conditions are filtered, and those that meet the conditions are filtered out.

3. Complex queries

The following introduces a SQL that tests thinking, which is very suitable for reviewing our existing SQL knowledge.

For some complex requirements, the difficulty of writing SQL will increase.

For example, now I ask to query info_type_id=3, and the two data with the highest page views, how to write it?

First look at all the data of info_type_id=3

SELECT * from info where info_type_id = 3;

For the two most viewed data, it is easy to think of sorting the views in reverse order, and then taking the first two.

SELECT * from info where info_type_id = 3 ORDER BY views desc;

So, how to take the first two? Just paginate directly.

SELECT * from info where info_type_id = 3 ORDER BY views desc LIMIT 0,2;

What if I want to use a SQL to get the top two pages with the most pageviews in each category?

I believe that the first reaction of many people is to use group by to group, but it seems to be more difficult.

In fact, as long as we keep in mind that the principle of select is to scan and match one by one, it is not difficult.

For example, we scan to the first entry:

Thinking, how to judge the views (pageviews) of this article are the first two digits of this type (info_type_id=3)?

Do we only need to make info_type_id=3 in this table (condition 1) and views greater than the views of this data (condition 2), and then require such data to be <= 1 (condition 3)?

Talk about why such data is required <= 1?

If it is equal to 1, it means that there is only one view larger than the current view, and adding this one is the two highest views, which meet the conditions.

If it is less than 1, it means that no one is larger than the current view, then the current one is the one with the highest views, which meets the conditions.

So, for the case of info_type_id=3, get sql

select t1.* from info t1 where  t1.info_type_id = 3
		and (select count(1) from info t2  where  t2.info_type_id = 3 and t2.views > t1.views ) <= 1

Success, and then rewrite it to all the cases:

SELECT t1.* from info t1
where (
	select count(1) from info t2  where  t2.info_type_id = t1.info_type_id 
		and t2.views > t1.views ) <= 1
order by info_type_id;

In short, we have to keep in mind that when selecting, it is executed one by one, and the where condition is to filter each item. Such complex SQL is not difficult to understand.

 I'm Brother Rabbit, follow me, don't get lost in learning programming~~

Guess you like

Origin blog.csdn.net/weixin_39570751/article/details/124111515