Sorting rows in database.
#1

Hello.
I have a little question.
I am trying to make log file in SQLite, so I can access it from not only SA-MP.
The problem is about sorting. I don't know how to display rows with biggest unix timestamp (recent added row).
To imagine you, what I am about, then take look at this table:

+---Table: Log
Date|xFrom|Text
123|abc|Yoyo
124|abb|Yoto
546|xxd|Hello
695|pwn|amx
+--------------|

If I will use this script:
pawn Код:
for(new i = 1; i < db_num_rows(db_Result); i++)
{
format(db_Query, 255, "Select xFrom from Log where rowid = 'i');
..
}
Will print:
Код:
abc
abb
xdd
pwn
amx
And now, what do I have to do if I want to display it in this order?
Код:
amx
pwn
xdd
abb
abc
Is there any way from pawn level to sort all data from table using ORDER BY expression?
Or do I have to do something else?

Greetz,
LetsOWN.
Reply
#2

Well, you could do it very easily by running the for loop in the opposite direction, I think:
pawn Код:
for(new i = db_num_rows(db_Result); i > 1; i--)
Reply
#3

Hey,
Yep, it might do the work If yes, then I'd just have to rebuild my page-building script (i mean showing 8 results / page) and if everything's will be ok.. You can count for rep+

Thanks,
Greetz,
LetsOWN.
Reply
#4

You're making it far more complex then it needs to be, just sort the data in the query.

PHP код:
SELECT xFrom 
FROM Log 
WHERE rowid 
'i'
ORDER BY Date DESC
use "ORDER BY Date ASC" to order the other way around.
Reply
#5

Quote:
Originally Posted by Sinner
Посмотреть сообщение
You're making it far more complex then it needs to be, just sort the data in the query.

PHP код:
SELECT xFrom 
FROM Log 
WHERE rowid 
'i'
ORDER BY Date DESC
use "ORDER BY Date ASC" to order the other way around.
I've tried this.
Not working

Greetz,
LetsOWN.
Reply
#6

Quote:
Originally Posted by LetsOWN[PL]
Посмотреть сообщение

I've tried this.
Not working

Greetz,
LetsOWN.
Can you show me how you did it? Because I know for a fact that that query will work just fine.

EDIT: You're not supposed to fetch every single row one by one depending on the rowid. Fetch them all at once (without the loop), then loop through the resulting set.
Reply
#7

Quote:
Originally Posted by Sinner
Посмотреть сообщение
EDIT: You're not supposed to fetch every single row one by one depending on the rowid. Fetch them all at once (without the loop), then loop through the resulting set.
I've done something silmilar. It's now working. However I am still working under separating results into pages (8 results per page), but I think I'll handle this on my own. Thank you any*wy

Greetz,
LetsOWN
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)