0

I am new in this cassandra database using with nodejs.

I have user_activity table. In this table data will insert based on user activity.

Also I have some user list. I need to fetch the data in that particular users and last record.

I don't interest to put the query in for loop. Have any other idea to achieve this?

Example Code:

var userlist = ["12", "34", "56"];
var query = 'SELECT * FROM user_activity WHERE userid IN ?';
server.user.execute(query, [userlist], {
    prepare : true
}, function(err, result) {
   console.log(results);
});

How to get the user lists for last one ?

Example:

user id = 12 - need to get last record;
user id = 34 - need to get last record;
user id = 56 - need to get last record;

I need to get these 3 records. Table Schema:

CREATE TABLE test.user_activity (
    userid text,
    ts timestamp,
    clientid text,
    clientip text,
    status text,
    PRIMARY KEY (userid, ts)
)
Aaron
  • 55,518
  • 11
  • 116
  • 132
RSKMR
  • 1,812
  • 5
  • 32
  • 73

2 Answers2

2

It is not possible if you use the IN filter.

If it is a single user_id filter you can apply order by. Of course you need a column for inserted/updated time. So query will be like this:

SELECT * FROM user_activity WHERE user_id = 12 ORDER BY updated_at LIMIT 1;
Aaron
  • 55,518
  • 11
  • 116
  • 132
Daniel A
  • 41
  • 5
1

You can put N value to get number of records

SELECT * FROM user_activity WHERE userid IN ? ORDER BY id DESC LIMIT N
abdulbarik
  • 6,101
  • 5
  • 38
  • 59
  • I am expecting results to get each users last records. – RSKMR Sep 30 '16 at 07:33
  • my query does to get 3 records in your case if put it `3` and that will get last three records of that user which you have mentioned.Isn't? – abdulbarik Sep 30 '16 at 07:50
  • No. Sorry if my content is clear means. My question is need to get those 3 users (each and everyone) results of last records – RSKMR Sep 30 '16 at 08:28