简体   繁体   中英

How to query adjacent rows in EF?

I have an app where I am displaying a timeline and I want the calendar event to show up in the middle of the chart. Therefore I would like to also return the previous 10 and next 10 rows in my query. For example let's say I have this table:

-------------------------------------------------
| id | user_id | date    | account_balance | action
-------------------------------------------------
.................................................
| 98  |    1   | 6/6/20  | 1524            | null
| 99  |    2   | 6/6/20  | 32              | null
| 100 |    1   | 6/7/20  | 1524            | null
| 101 |    2   | 6/7/20  | 32              | null
| 102 |    1   | 6/8/20  | 1524            | null
| 103 |    2   | 6/9/20  | 32              | null
| 104 |    1   | 6/9/20  | 1524            | null
| 105 |    2   | 6/10/20 | 32              | null
| 106 |    1   | 6/10/20 | 1560            | deposit
| 107 |    2   | 6/11/20 | 32              | null
| 108 |    1   | 6/11/20 | 1560            | null
| 109 |    2   | 6/12/20 | 32              | null
| 110 |    1   | 6/12/20 | 1560            | null
| ...............................................

How can I query the latest deposit ( 'WHERE action = deposit and user_id = 1' ) and return row 106 as well as...92,94,96,98,100,102,104 and 108,110,112,114... Is there a way to achieve this with a single query with EF?

For your data, and as long there is oly one id

 CREATE TABLE account ( `id` INTEGER, `user_id` INTEGER, `date` DATETIME, `account_balance` INTEGER, `action` VARCHAR(7) ); INSERT INTO account (`id`, `user_id`, `date`, `account_balance`, `action`) VALUES ('98', '1', '6/6/20', '1524', 'null'), ('99', '2', '6/6/20', '32', 'null'), ('100', '1', '6/7/20', '1524', 'null'), ('101', '2', '6/7/20', '32', 'null'), ('102', '1', '6/8/20', '1524', 'null'), ('103', '2', '6/9/20', '32', 'null'), ('104', '1', '6/9/20', '1524', 'null'), ('105', '2', '6/10/20', '32', 'null'), ('106', '1', '6/10/20', '1560', 'deposit'), ('107', '2', '6/11/20', '32', 'null'), ('108', '1', '6/11/20', '1560', 'null'), ('109', '2', '6/12/20', '32', 'null'), ('110', '1', '6/12/20', '1560', 'null');
 (SELECT * FROM account a WHERE id <= (SELECT id FROM account WHERE user_id = 1 AND action = 'deposit' ORDER BY id LIMIT 1) AND user_id = 1 ORDER BY id DESC LIMIT 11) UNION ALL (SELECT * FROM account a WHERE id > (SELECT id FROM account WHERE user_id = 1 AND action = 'deposit' ORDER BY id LIMIT 1) AND user_id = 1 ORDER BY id ASC LIMIT 10) ORDER BY id;
 id | user_id | date | account_balance | action --: | ------: |:------------------ |  --------------: |:------ 98 |  1 |  0006-06-20 00:00:00 |  1524 |null 100 |  1 |  0006-07-20 00:00:00 |  1524 |null 102 |  1 |  0006-08-20 00:00:00 |  1524 |null 104 |  1 |  0006-09-20 00:00:00 |  1524 |null 106 |  1 |  0006-10-20 00:00:00 |  1560 |  deposit 108 | 1 |  0006-11-20 00:00:00 |  1560 |  null 110 |  1 |  0006-12-20 00:00:00 |  1560 |  null 

db<>fiddle here

With one id this works fine, but with many ids you can use a stored procedure

DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `get_account_data`(IN _user_id INt, IN _limit INT, IN _action TEXT)
BEGIN
    DECLARE finished INTEGER DEFAULT 0;
    DECLARE _sqltext LONGTEXT;
    DECLARE _selid INTEGER DEFAULT 0;
    DECLARE _counter INTEGER DEFAULT 0;
        -- declare cursor for row id
    DEClARE curid 
        CURSOR FOR 
            SELECT id FROM account WHERE user_id = _user_id AND action = _action;

    -- declare NOT FOUND handler
    DECLARE CONTINUE HANDLER 
        FOR NOT FOUND SET finished = 1;
        
        SET _sqltext = '';
    OPEN curid;

    getid: LOOP
        FETCH curid INTO _selid;
        IF finished = 1 THEN 
            LEAVE getid;
        END IF;
        -- build id list
        IF _counter > 0 THEN
            SET _sqltext = CONCAT(_sqltext, " UNION ALL ");
        ELSE
            SET _counter = 1;
         END IF;
            
        SET _sqltext = CONCAT(_sqltext,"(SELECT * FROM account a WHERE id <= ",_selid," AND user_id = ",_user_id," ORDER BY id DESC LIMIT ", _limit + 1,")");
        SET _sqltext = CONCAT(_sqltext, " UNION ALL ");
        SET _sqltext = CONCAT(_sqltext,"(SELECT * FROM account a WHERE id > ",_selid," AND user_id = ",_user_id," ORDER BY id ASC LIMIT ", _limit ,")");
    END LOOP getid;
    CLOSE curid;        
    SET _sqltext = CONCAT(_sqltext, " ORDER BY id;");
    SET @sql = _sqltext;
    PREPARE stmt1 FROM @sql;
    EXECUTE stmt1;
    DEALLOCaTE PREPARE stmt1;
END//
DELIMITER ;

When you now call this, you get all the rows that have the action for that user and with the 5 rows

CALL get_account_data(1,5,'deposit');

BUT caution the action text should be checked against a whitelist with allowed text, so that sql injection can be prevented

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM