View previous topic :: View next topic |
Author |
Message |
ShaDoOoW
Joined: 20 Aug 2005 Posts: 584
|
Posted: Sun Jul 18, 2010 14:33 Post subject: MySQL and num rows. |
|
|
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 |
|
|
ShaDoOoW
Joined: 20 Aug 2005 Posts: 584
|
Posted: Sun Jul 18, 2010 14:48 Post subject: |
|
|
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 |
|
|
Baaleos
Joined: 02 Sep 2007 Posts: 830
|
Posted: Sun Jul 18, 2010 18:30 Post subject: |
|
|
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 |
|
|
ShaDoOoW
Joined: 20 Aug 2005 Posts: 584
|
Posted: Sun Jul 18, 2010 18:34 Post subject: |
|
|
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 |
|
|
axs
Joined: 11 Feb 2005 Posts: 76
|
Posted: Sun Jul 18, 2010 19:23 Post subject: |
|
|
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 |
|
|
Zebranky
Joined: 04 Jun 2006 Posts: 415
|
Posted: Sun Jul 18, 2010 21:06 Post subject: |
|
|
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 |
|
|
ShaDoOoW
Joined: 20 Aug 2005 Posts: 584
|
Posted: Sun Jul 18, 2010 21:19 Post subject: |
|
|
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 |
|
|
axs
Joined: 11 Feb 2005 Posts: 76
|
Posted: Sun Jul 18, 2010 21:19 Post subject: |
|
|
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 |
|
|
ShaDoOoW
Joined: 20 Aug 2005 Posts: 584
|
Posted: Sun Jul 18, 2010 21:21 Post subject: |
|
|
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 |
|
|
|