Skip to main content

Posts

Showing posts from April, 2015

delete all the values with in a group keeping only the topmost entry in each group in mysql

sometimes you're caught up in a situation where you have multiple entries against an id and you want to keep only single entry against each. Table structure would be something like this: Table `temp` Entries: user_id tstamp source 100 4/26/2015 app 100 4/26/2014 web 100 4/25/2015 app 101 4/26/2015 app 100 4/23/2015 app 101 4/20/2015 web 100 4/1/2015 app 200 4/26/2015 app 200 4/2/2015 app Desired Output: user_id tstamp source 100 4/26/2015 app 100 4/26/2014 web 101 4/26/2015 app 200 4/26/2015 app Now, I want to delete the rows where source is `app` and keep the entry with max tstamp only with in a group. Here's the sample query to achieve the same: DELETE tmp FROM `temp` tmp join (select user_id, max(tstamp) as ts from `temp` where user_id in (select user_id from `temp` where source = 'app' group by user_id having count(user_id)>2) and source = 'app' group by user_id )t on tmp.user_id = t.user_id and tmp.tstamp != t.t

how to access a private member outside the class in php (php Hack)

If you want to access a private variable/method outside the class... here is a trick to do the same: class A { private $notVisibleOutside = 1; } $a = new ReflectionClass("A"); $property = $class->getProperty("notVisibleOutside "); $property->setAccessible(true); $a1 = new A(); echo $property->getValue($a1); // Work So, the basic idea is to - create a reflection class in php - make the property or method accessible - use that property to access the private variables/methods