Hello, is there any comand like RANK or COUNT in php? more particularly when selecting from database: SELECT * FROM table1 WHERE category = 4 AND id = 12345 LIMIT 1 PHP: 1. Lets say that item has id, category and VIEWS attributes.. is it possible to tell how that item with that particular ID ranks by views for category 4? 2. Is there a way to count the number of entries in category 4, without having to do something rediculous like selecting all items from table where category = 4 and doing a loop with $count++; any help greatly appreciated
1. SELECT * FROM table1 WHERE category = 4 ORDER BY views DESC 2. SELECT COUNT(*) FROM table1 WHERE category = 4 This is more to do with databases than PHP.
thats more mysql than php.. im not too sure about number 1, but for 2, you can do SELECT count(id) FROM table1 WHERE category = 4
It is also good practice to add a LIMIT to your results. If you have 250 000 lines in your table and are only interested in returning the first, say, 20 for your web page, then it makes no sense in selecting the entire set every time you run the query. SELECT * FROM table1 WHERE category = 4 ORDER BY views DESC LIMIT 0,20
thanks a lot for the help guys, but i guess what i am trying to do for the first one is... lets say there are 5 values in category 4 with VIEWS = {10,20,30(id=1),40,50} so it would be somethign like RANK by id where cat = 4 and id = 1 order DESC and it would return 3, since it is the third highest value Thanks for help