Sunday, April 17, 2005

Faster way of getting random rows in mysql instead of using order by rand()

1. create a new indexed column (= column_x) that's integer incremented for each table
2. select count(*) to get # of rows
3. use the app to generate X # of integers between 1 and last row
4. select * from table where column_x in (rand1, rand2, rand3, ...)

1 comment:

Ram said...

Age calculation using mysql query

This query calculate the age in the form of Year and Month
-------------------------------------------------------------------------------
SELECT concat(floor(datediff(curdate(),'2008-12-31')/365),'Y ',floor((datediff(curdate(),'2008-12-31')%365)/30),'M') as age;
-------------------------------------------------------------------------------
---------------
age
-----------------
0Y 1M
--------


There is another query which calculate the age in the form of Year, Month, Day

---------------------------------------------------------
SELECT concat(floor(datediff(curdate(),'2008-12-31')/365) ,'Y ',floor((datediff(curdate(),'2008-12-31')%365)/30) ,'M ',
(datediff(curdate(),'2008-12-31')-(floor(datediff(curdate(),'2008-12-31')/365)*365+floor((datediff(curdate(),'2008-12-31')%365)/30)*30)),'D ') as age;
---------------------------------------------------------
------------
age
------------
0Y 1M 12D
------------


Where y = Year,M = Month, D=Day