Let's talk about the principle first:
There are previous articles and next articles (previous articles and next articles), which must be an effect in a query set (list) obtained under the scenario of search conditions and fixed sorting rules.
1. We use rownum (a self increasing label in order) to query the result set under these two conditions (search condition where and ordering order)
2. Query the rownum value x of the target uuid
3. Query the previous item and the next item: the uuid with rownum = x-1 gets the previous item and the uuid with rownum = x+1 gets the next item
Practical application:
A query with fixed search criteria and fixed sorting:
SELECT bn.* FROM base_notice bn LEFT JOIN ( SELECT GROUP_CONCAT(userinfo.STR_NAME) AS toUserNames, GROUP_CONCAT(touser.STR_GUID) AS addresseeGuids, GROUP_CONCAT(touser.STR_USER) AS touserIds, GROUP_CONCAT(touser.is_read) AS isReads, notice.STR_GUID AS noticeId FROM base_notice notice LEFT JOIN base_notice_addressee touser ON touser.STR_NOTICE_GUID = notice.STR_GUID LEFT JOIN base_user userinfo ON touser.str_user = userinfo.STR_GUID WHERE notice.STR_CREATE_USER_ID = '1367646365085077505' AND notice.STR_TYPE = 2 AND ( touser.IS_DELETE = 0 AND touser.IS_ENABLE = 1 ) GROUP BY notice.STR_GUID ) tempT1 ON bn.STR_GUID = tempT1.noticeId WHERE bn.IS_DELETE = 0 AND bn.IS_ENABLE = 1 AND bn.STR_CREATE_USER_ID = '1367646365085077505' ORDER BY bn.DT_GET_DATE DESC
Add rownum: (compare the above sql with the following sql and add -- mark the operation)
Note: in practical application, the uuid of the previous item and the uuid of the next item are usually obtained. Here, I added rowNum to the result set and only queried the uuid
This code only observes the actual operation of the mark position. The effects and results without "–" are attached below:
-- SELECT -- rownum AS rowNum, -- STR_GUID AS strGuid -- FROM -- ( SELECT -- @rownum :=@rownum + 1 AS rownum, bn.* FROM -- (SELECT @rownum := 0) r, base_notice bn LEFT JOIN ( SELECT GROUP_CONCAT(userinfo.STR_NAME) AS toUserNames, GROUP_CONCAT(touser.STR_GUID) AS addresseeGuids, GROUP_CONCAT(touser.STR_USER) AS touserIds, GROUP_CONCAT(touser.is_read) AS isReads, notice.STR_GUID AS noticeId FROM base_notice notice LEFT JOIN base_notice_addressee touser ON touser.STR_NOTICE_GUID = notice.STR_GUID LEFT JOIN base_user userinfo ON touser.str_user = userinfo.STR_GUID WHERE notice.STR_CREATE_USER_ID = '1367646365085077505' AND notice.STR_TYPE = 2 AND ( touser.IS_DELETE = 0 AND touser.IS_ENABLE = 1 ) GROUP BY notice.STR_GUID ) tempT1 ON bn.STR_GUID = tempT1.noticeId WHERE bn.IS_DELETE = 0 AND bn.IS_ENABLE = 1 AND bn.STR_CREATE_USER_ID = '1367646365085077505' ORDER BY bn.DT_GET_DATE DESC -- ) tempT
SELECT rownum AS rowNum, STR_GUID AS strGuid FROM ( SELECT @rownum :=@rownum + 1 AS rownum, bn.* FROM (SELECT @rownum := 0) r, base_notice bn LEFT JOIN ( SELECT GROUP_CONCAT(userinfo.STR_NAME) AS toUserNames, GROUP_CONCAT(touser.STR_GUID) AS addresseeGuids, GROUP_CONCAT(touser.STR_USER) AS touserIds, GROUP_CONCAT(touser.is_read) AS isReads, notice.STR_GUID AS noticeId FROM base_notice notice LEFT JOIN base_notice_addressee touser ON touser.STR_NOTICE_GUID = notice.STR_GUID LEFT JOIN base_user userinfo ON touser.str_user = userinfo.STR_GUID WHERE notice.STR_CREATE_USER_ID = '1367646365085077505' AND notice.STR_TYPE = 2 AND ( touser.IS_DELETE = 0 AND touser.IS_ENABLE = 1 ) GROUP BY notice.STR_GUID ) tempT1 ON bn.STR_GUID = tempT1.noticeId WHERE bn.IS_DELETE = 0 AND bn.IS_ENABLE = 1 AND bn.STR_CREATE_USER_ID = '1367646365085077505' ORDER BY bn.DT_GET_DATE DESC ) tempT
Then suppose we need to query the previous and next item of uuid = "100", and we only need to add the where condition query after the above sql
rowNum value for where strGuid='100 '.
where tempT.STR_GUID='100'
We can see that strGuid = '100' and his rownum=2 are easy. We know that his previous rowNum=1 and his next rowNum=3
So we modify the conditions of the above sql statement
where rownum in (1,3);
From the result set, we can see that the previous uuid and the next uui are available
rownum=1 of the uuid currently checked, that is, the result set of the first item
where rownum in (0,2);
rownum=3 of the uuid currently checked, that is, the result set of the last one
where rownum in (2,4);
How to distinguish the first one from the last one?
We use the query condition that the currently checked uuid rownum=x
Get the result set rownum = X-1. Strguid is not null, that is, the previous value gets the result set rownum= x+1
strGuid is not null, that is, the next item has a value
Previous null next non null: currently the first previous non null next non null: not the middle of the first and last non null
Next null: it is currently the last previous null. Next null: it is not a query record within the current condition.
Previous uuid | Next uuid | Currently queried uuid conclusion |
---|---|---|
null | Non null | Article 1 |
Non null | Non null | Not the middle of the headline and the last one |
Non null | null | Last one |
null | null | : query records not in the current condition |
above. Did you learn to waste!?