Best way to create a dynamic of this is through a database control, where each change of user status would be recorded the status, which user belongs and the date of the change.
An example of a bank schema:
CREATE TABLE IF NOT EXISTS `user_status` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT '',
`name` VARCHAR(45) NOT NULL COMMENT '',
`status` VARCHAR(45) NULL COMMENT '',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '',
PRIMARY KEY (`id`) COMMENT '')
ENGINE = InnoDB
Okay, we set up the database. In this database will be recorded each change of user status, along with the date that will be filled automatically with CURRENT_TIMESTAMP
.
Remembering that in your application environment you will need to create a foreign key for the ID
user. Referencing the record to it.
With each status drive in your application you will generate an Insert query for this table:
INSERT INTO `user_status` (`name`, `status`) VALUES ('FulanoA', 'está online')
This way will be registered status change FulanoA
for está online
.
To recover these status just run a query in this table, searching for the last status drives ordernando by field created_at
;
SELECT `us`.`name`, `us`.`status` FROM `user_status` us us ORDER BY `us`.`created_at` DESC LIMIT 4
This query will return the last 4 records ordered by the change date, will be output the way you want.
With each new status change the last record will leave the table and a new one will enter at the beginning of it, same game that demonstrated in the example with FulanoZ
.
Considerations
I will make some considerations taking into account this modeling.
Database
It’s a little expensive to performance these constant hit’s in the bank, with each change you register and show, imagine this on a large scale. Cost of I/O will be high for your application. Imagine this in on-demand service?
So what better way out?
Analysis of the need for this interaction, for what purpose and value of this functionality x its cost.
You need to analyze and see if the demand is worth the cost of performance, just to show users status movement history.
Alternatives
Working with these records in a bank is not well recommended, but if you want to use a bank for these drives recommend the noSQL
which will have the lowest cost for these frequent queries.
We also have the Redis
Redis is a Nosql database that works with the idea of key value. The key as well as its value can be almost anything, like a Hash in Ruby or a Map in Java. Being a very easy way to save data like user cache, or task queues.
Which is the most advisable for this task, which is precisely noSQL
.
Well, I owe a little more code, since this doubt is not very much related to how to do and yes how it works. All alternatives addressed here can be made in several languages, depends on the analysis of your project and demand.
I recommend studying the alternatives put here, because this is really something that will eat your performance with flour rsrs if not well modeled.
Well, I hope you’ve cleared something up.
You can make an auxiliary table, which records the accesses and sorts in order of access.
– Sr. André Baill
Are you saying that
FulanoA
pass the position2
,FulanoB
pass the position3
and so on ?– Edilson
yes! always returned ex: 3 turns 2, 2 turns 1 etc...
– FRNathan13
But then it wouldn’t be a bit of a hassle ? What is the purpose of the table after all ?
– Edilson
You can sort by the field you want first:
ORDER BY FIELD(status, 1, 2, 3, 4, 5)
. Because I see no point in wanting to remove one and put another in place. What’s your idea?– Ivan Ferrer
The organization of the records on the list is done by the time/min they happen, right? type, the more at the top, but recent is action, right?
– Intruso
But if it is of type ordered in ascending order to the input time, just update the field responsible for recording time whenever the user enters, and then the display order, would be made during the output or processing of the data in the table, and not permanently by SQL. Because this kind of system you’re asking for is more viable for CMS and things like that. So I repeat the question. What is your main goal in this structure ?
– Edilson