Return a value if no record is found

Encapsulate the query in a sub-query to transform “no row” to a NULL value. I tested and verified this with PostgreSQL, SQL Server and MySQL. Also works with SQLite. SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id; In Oracle you have to select from the dummy 1-row table DUAL like this: SELECT … Read more

Screen Video Record of Current Activity Android

Since Lollipop we can use the Media Projection API ! (API 21+) Here is the following code that I use for recording, Note that we first need to get the user permissions for that 😉 private static final int CAST_PERMISSION_CODE = 22; private DisplayMetrics mDisplayMetrics; private MediaProjection mMediaProjection; private VirtualDisplay mVirtualDisplay; private MediaRecorder mMediaRecorder; @Override … Read more

How to get the last N records in mongodb?

If I understand your question, you need to sort in ascending order. Assuming you have some id or date field called “x” you would do … .sort() db.foo.find().sort({x:1}); The 1 will sort ascending (oldest to newest) and -1 will sort descending (newest to oldest.) If you use the auto created _id field it has a … Read more

SQL – Update multiple records in one query

Try either multi-table update syntax UPDATE config t1 JOIN config t2 ON t1.config_name=”name1″ AND t2.config_name=”name2″ SET t1.config_value=”value”, t2.config_value=”value2″; Here is a SQLFiddle demo or conditional update UPDATE config SET config_value = CASE config_name WHEN ‘name1’ THEN ‘value’ WHEN ‘name2’ THEN ‘value2’ ELSE config_value END WHERE config_name IN(‘name1’, ‘name2’); Here is a SQLFiddle demo