Quantcast
Channel: Oracle, MySQL, Sybase, Informix and other databases
Viewing all articles
Browse latest Browse all 1350

When username changes on table register get only last username in mysql

$
0
0

Hi all,

This is my table tbl_register on MySQL database

+---------------------+------------------+---------------------+-----+| sDateTime           | sRegisterAccount | sNewRegisterAccount | sID |+---------------------+------------------+---------------------+-----+|2019-11-2718:52:00|116019|NULL|1||2017-08-0514:01:00|030270|116019|2||2020-04-0114:36:00|542116|NULL|3||2018-10-3018:36:00|000647|542116|4||2019-03-0714:19:00|115286|NULL|5||2020-02-1723:16:00|170134|NULL|6|
| 2016-02-24 11:37:33 | 052994 | 583725 | 7 |
+---------------------+------------------+---------------------+-----+

On this table tbl_register are also recorded any username changes in the column sNewRegisterAccount

e.g.

+---------------------+------------------+---------------------+-----+| sDateTime           | sRegisterAccount | sNewRegisterAccount | sID |+---------------------+------------------+---------------------+-----+|2020-04-0114:36:00|542116|NULL|3||2018-10-3018:36:00|000647|542116|4|+---------------------+------------------+---------------------+-----+

in the column sRegisterAccount the old account value 000647

in the column sNewRegisterAccount the new account value 542116

I need get only last value of sNewRegisterAccount as return

+---------------------+------------------+---------------------+-----+| sDateTime           | sRegisterAccount | sNewRegisterAccount | sID |+---------------------+------------------+---------------------+-----+|2019-11-2718:52:00|116019|NULL|1||2020-04-0114:36:00|542116|NULL|3||2019-03-0714:19:00|115286|NULL|5||2020-02-1723:16:00|170134|NULL|6|
| 2016-02-24 11:37:33 | 052994 | 583725 | 7 |
+---------------------+------------------+---------------------+-----+

I've tried without success this query

mysql>SELECT
    A.*FROM`tbl_register` AINNERJOIN(SELECT
        sRegisterAccount,
        MAX(sDateTime) LastDatetimeForNodeFROM`tbl_register`GROUPBY
        sRegisterAccount) B ON A.sRegisterAccount = B.sRegisterAccountAND A.sDateTime = B.LastDatetimeForNode;+---------------------+------------------+---------------------+-----+| sDateTime           | sRegisterAccount | sNewRegisterAccount | sID |+---------------------+------------------+---------------------+-----+|2019-11-2718:52:00|116019|NULL|1||2017-08-0514:01:00|030270|116019|2||2020-04-0114:36:00|542116|NULL|3||2018-10-3018:36:00|000647|542116|4||2019-03-0714:19:00|115286|NULL|5||2020-02-1723:16:00|170134|NULL|6|
| 2016-02-24 11:37:33 | 052994 | 583725 | 7 |+---------------------+------------------+---------------------+-----+6rowsinset

what am I doing wrong?

please, can you help me?

expected results

+---------------------+------------------+---------------------+-----+
| sDateTime           | sRegisterAccount | sNewRegisterAccount | sID |+---------------------+------------------+---------------------+-----+
| 2019-11-27 18:52:00 | 116019           | NULL                |   1 |
| 2020-04-01 14:36:00 | 542116           | NULL                |   3 |
| 2019-03-07 14:19:00 | 115286           | NULL                |   5 |
| 2020-02-17 23:16:00 | 170134           | NULL                |   6 |
| 2016-02-24 11:37:33 | 052994 | 583725 | 7 |+---------------------+------------------+---------------------+-----+

it should also extract the row number 7, because the user have changed username from052994 to 583725… but no longer authenticated with new username 583725

structure table tbl_register below

DROPTABLEIFEXISTS`tbl_register`;CREATETABLE`tbl_register`(`sDateTime` datetime DEFAULTNULL,`sRegisterAccount` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULTNULL,`sNewRegisterAccount` varchar(255)DEFAULTNULL,`sID` int(11)NOTNULL AUTO_INCREMENT,PRIMARYKEY(`sID`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ------------------------------ Records of tbl_register-- ----------------------------INSERTINTO`tbl_register`VALUES('2019-11-27 18:52:00','116019',null,'1');INSERTINTO`tbl_register`VALUES('2017-08-05 14:01:00','030270','116019','2');INSERTINTO`tbl_register`VALUES('2020-04-01 14:36:00','542116',null,'3');INSERTINTO`tbl_register`VALUES('2018-10-30 18:36:00','000647','542116','4');INSERTINTO`tbl_register`VALUES('2019-03-07 14:19:00','115286',null,'5');INSERTINTO`tbl_register`VALUES('2020-02-17 23:16:00','170134',null,'6');
INSERTINTO`tbl_register`VALUES('2016-02-24 11:37:33','052994','583725','7');

Viewing all articles
Browse latest Browse all 1350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>