logo logo

 Back to main page

The NWNX Community Forum

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
MySQL and num rows.

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related
View previous topic :: View next topic  
Author Message
ShaDoOoW



Joined: 20 Aug 2005
Posts: 584

PostPosted: Sun Jul 18, 2010 14:33    Post subject: MySQL and num rows. Reply with quote

Hey, I need to know how many rows I selected from last SELECT statement. Atm I added new function into ODBC dll which calls mysql_num_rows, but if there is any method to do it via MySQL command like SELECT, I would prefer it... Does anyone know?
_________________
Community Patch / NWNX Patch / NWNX Files / NWNX Connect
Back to top
View user's profile Send private message
ShaDoOoW



Joined: 20 Aug 2005
Posts: 584

PostPosted: Sun Jul 18, 2010 14:48    Post subject: Reply with quote

got it,
Code:
SELECT COUNT(DISTINCT baseitem) FROM treasure_list WHERE level='1' AND type='1';

_________________
Community Patch / NWNX Patch / NWNX Files / NWNX Connect
Back to top
View user's profile Send private message
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Sun Jul 18, 2010 18:30    Post subject: Reply with quote

U sure??
I thought that query just returns the amount of how many different valued rows there are for that specific column.

Eg if you had two rows with same value in baseitem 'in' each it would only count it once, because it only exists distinctly once.

I've used the distinct method b4 when I made a web interface allowing me to create items via choosing the item properties. I used select distinct item properties, to get them to appear once per unique value.
Back to top
View user's profile Send private message
ShaDoOoW



Joined: 20 Aug 2005
Posts: 584

PostPosted: Sun Jul 18, 2010 18:34    Post subject: Reply with quote

Yes I am sure. The distinc value will filter the same values for baseitem, I mean I have there 151 values, but baseitem is mostly the same, thats why I used distict. With that it returns 21 rows aka "how many different baseitems are there". The COUNT(DISTINCT baseitem) returns 21 and thats exactly what I need.

btw for random one I used this statement
Code:
SELECT DISTINCT baseitem FROM treasure_list WHERE level='1' AND type='1' LIMIT "+IntToString(Random(nNumBaseItems))+",1";

EDIT: error in statement
_________________
Community Patch / NWNX Patch / NWNX Files / NWNX Connect


Last edited by ShaDoOoW on Sun Jul 18, 2010 21:17; edited 1 time in total
Back to top
View user's profile Send private message
axs



Joined: 11 Feb 2005
Posts: 76

PostPosted: Sun Jul 18, 2010 19:23    Post subject: Reply with quote

For random selection you can try to use:
Code:
SELECT COUNT(DISTINCT baseitem) FROM treasure_list WHERE level='1' AND type='1' ORDER BY RAND() LIMIT 1;
Back to top
View user's profile Send private message
Zebranky



Joined: 04 Jun 2006
Posts: 415

PostPosted: Sun Jul 18, 2010 21:06    Post subject: Reply with quote

ShaDoOoW's approach might be better, especially on larger sets, since RAND() will run once for every row in the table.
_________________
Win32 SVN builds: http://www.mercuric.net/nwn/nwnx/

<Fluffy-Kooshy> NWNx plugin is to this as nuclear warheads are to getting rid of fire ants.

<ThriWork> whenever I hear nwn extender, I think what does NWN need a penis extender for?
Back to top
View user's profile Send private message Visit poster's website
ShaDoOoW



Joined: 20 Aug 2005
Posts: 584

PostPosted: Sun Jul 18, 2010 21:19    Post subject: Reply with quote

axs wrote:
For random selection you can try to use:
Code:
SELECT DISTINCT baseitem FROM treasure_list WHERE level='1' AND type='1' ORDER BY RAND() LIMIT 1;
Nice, this way I dont event have to find out how many values there is right?

Zebranky: like it then automatically loop all rows fetched and do some random math in order to determine which one will be at first place?
_________________
Community Patch / NWNX Patch / NWNX Files / NWNX Connect


Last edited by ShaDoOoW on Sun Jul 18, 2010 21:20; edited 1 time in total
Back to top
View user's profile Send private message
axs



Joined: 11 Feb 2005
Posts: 76

PostPosted: Sun Jul 18, 2010 21:19    Post subject: Reply with quote

Zebranky wrote:
ShaDoOoW's approach might be better, especially on larger sets, since RAND() will run once for every row in the table.
Yeah, right, depends on use. For one row it is faster.

ShaDoOoW wrote:
Nice, this way I dont event have to find out how many values there is right?
Yes.

And its some times better than use multiple query if you want to get more than one random row.


Last edited by axs on Sun Jul 18, 2010 21:22; edited 3 times in total
Back to top
View user's profile Send private message
ShaDoOoW



Joined: 20 Aug 2005
Posts: 584

PostPosted: Sun Jul 18, 2010 21:21    Post subject: Reply with quote

Guess I will stick in the count + limit then, but still very usefull info. Thanks.
_________________
Community Patch / NWNX Patch / NWNX Files / NWNX Connect
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group