| View previous topic :: View next topic |
| Author |
Message |
Reserve Voice
Joined: 18 Apr 2005 Posts: 6 Location: Nottingham, UK
|
Posted: Wed Jan 18, 2006 5:15 am Post subject: How to search a list of results from an sql query? |
|
|
I’m trying to write a script that queries a database, sorts the results and then searches the results for a match.
The select query will be something like
| Code: | mysqlsel $mysql "SELECT * FROM sql_table where last_event < $time"
set mycount [mysqlresult $mysql rows?]
mysqlmap $mysql {id name score kills} {
} |
I have googled and searched the forum and found a few matches but I can’t get what I'm trying to do to work and don’t seem to be getting any error messages to help work out why it’s not working.
Once I have the results from the query I need to build a list of id, name, score and kills and sort first on score (DESC) and then on kills (DESC). That bit of the problem I can do with the actual select statement. The bit I'm having problems with is I need to somehow generate an index number for each result starting from 1 and ending with the last result to the query as follows
| Code: | Index name score kills
1 name1 100 20
2 name4 95 15
3 name2 82 10
4 name5 82 8
5 name3 60 5
|
From this list I then need to do a search for a particular name and select that whole row but the important part of the row will be the index number for that row. I’ve seen references to lsearch and think this may be the way to do it but in not sure how to actually implement it.
Can anyone offer me any help or guidance with this script?
TIA
Regards
Reserve |
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Thu Jan 19, 2006 1:25 am Post subject: |
|
|
build your result set as list of lists:
| Code: |
...
mysqlmap $db {id name score kills} {
...
lappend ::result [list $id $name $score $kills]
...
}
|
then you'll be able to sort by index in sublist:
| Code: |
proc foo {...} {
...
# sort by name
set ::result [lsort -index 1 $::result]
...
}
|
refer to lsort manpage for more info and examples _________________ connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|