Monday, July 30, 2012

How to find second highest salary in MySQL


if you have MySQL table like below image


if you want to fetch second highest salary of employee following query will be useful for you.
select sal from emp group by sal order by sal desc Limit 1,1

your output will be like below image


if you want to fetch all employee which have second highest salary following query will be useful for you.
select * from emp where sal=(select sal from emp group by sal order by sal desc Limit 1,1)

your output will be like below image


One more query
SELECT * FROM  `users` WHERE  `id` = ( SELECT MAX( id ) -1 FROM  `users` )

SELECT * FROM  `users` WHERE  `id` < ( SELECT MAX( id ) -1 FROM  `users` ) ORDER BY id DESC LIMIT 1


Read More »

Sunday, July 22, 2012

How to delete all inside folder

This function is useful to delete all inner folder of given path of folder.
Lets explain in example:
if you have folder which name is user_1  and user_1 have lots of folder like (my_album, my_profile,friends_photo etc.)
if you want to delete folder (my_albummy_profile,friends_photo etc.) call function like belew
<?php
deleteDirectory($dir); // $dir = path of user_1 folder
?>


function deleteDirectory($dir) {
if (!file_exists($dir)) return true;
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') continue;
if (!deleteDirectory($dir . "/" . $item)) {
chmod($dir . "/" . $item, 0777);
if (!deleteDirectory($dir . "/" . $item)) return false;
};
}
return rmdir($dir);
}

cheers!
Read More »

Saturday, July 21, 2012

Date after 10 days


$your_date = '2012-07-21'; $date_after_ten_days = date("Y-m-d",strtotime(date("Y-m-d", strtotime($your_date)) . " +10 day")); echo $date_after_ten_days; //output 2012-07-31
Read More »