-1

Is there a way how to fetch all data from table where number is higher than something.

In my case I am using UNIX time in database to be able to easily compare data. Right now I would like to count every login which is higher then current UNIX time minus 24 hours.

Is there a way how I can write it? I was thinking about something like this but not sure how can I write SQL statement with < or >.

function getDailyOnline()
{
    return $this->db->table('users')
        ->where('last_login', $post->id);
}   

I need SQL command, don't need to use Nette Framework as I do in my "example".

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Andurit
  • 5,612
  • 14
  • 69
  • 121

4 Answers4

4

You can simply try this:

$this->db->table('users')->where('last_login > ', $post->id);
Riad
  • 3,822
  • 5
  • 28
  • 39
  • Is this missing a `?` in the `where` clause? – tadman Jan 12 '15 at 17:17
  • It's nette database. It allows you to not write ?, it will append them automatically. – hrach Jan 12 '15 at 19:58
  • This is actually really good point, thanks for that. how ever at the end based on this and other questions i create my final answer: `return $this->db->fetchPairs('SELECT COUNT(*) last_login FROM users WHERE last_login > UNIX_TIMESTAMP() - (24 * 60 * 60)');` – Andurit Jan 13 '15 at 08:46
2
SELECT COUNT(*) AS cnt
  FROM users
 WHERE last_login > (UNIX_TIMESTAMP() - (24 * 60 * 60));
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • please edit your answer and include some explanation about why this solves the OP problem. Code-only answers are sometimes good enough, but code+explanation answers are always better. – Barranka Jan 12 '15 at 17:28
  • @Barranka I don't see anything that would specifically require explanation; it's pretty basic SQL that directly responds to OP's question. If OP has any particular problems with my answer, I'd be happy to explain, or retract if necessary. – shmosel Jan 12 '15 at 17:44
  • Thumb up, your answer was usefull for me :) – Andurit Jan 13 '15 at 08:47
2

you want to do something like this:

SELECT COUNT *
  FROM users
  WHERE last_login > (UNIX_TIMESTAMP() - (24 * (3600)));

This will select all the row with a last_login older than 24 hours(1day).

william.eyidi
  • 2,315
  • 4
  • 27
  • 39
0

I just tried and you can go for this solution:

$this->db->table("users")
    ->where('last_login > ', $post->id);

cheers

william.eyidi
  • 2,315
  • 4
  • 27
  • 39