<p>I'm using MySQL in particular, but I'm hoping for a cross-vendor solution.  I'm using the NOW() function to add a timestamp as a column for each record.</p>

<pre><code>INSERT INTO messages 
(typeId, messageTime, stationId, message) 
VALUES 
(?, NOW(), ?, ?)
</code></pre>

[{'answer_id': 59437, 'author': 'Alexander Morland', 'author_id': 4013, 'author_profile': 'https://Stackoverflow.com/users/4013', 'pm_score': 3, 'selected': True, 'text': '<pre><code>SELECT * FROM messages WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) &lt;= messageTime\n</code></pre>\n'}, {'answer_id': 59443, 'author': 'Martynnw', 'author_id': 5466, 'author_profile': 'https://Stackoverflow.com/users/5466', 'pm_score': 2, 'selected': False, 'text': '<p>The SQL Server query is:</p>\n\n<pre><code>Select *\nFrom Messages\nWhere MessageTime &gt; DateAdd(dd, -1, GetDate())\n</code></pre>\n\n<p>As far as I can tell the (untested!) MySQL equivalent is</p>\n\n<pre><code>Select *\nFrom Messages\nWhere MessageTime &gt; ADDDATE(NOW(), INTERVAL -1 DAY)\n</code></pre>\n'}, {'answer_id': 59459, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 0, 'selected': False, 'text': "<p>There is no cross database solution, as most of them have their own date handling (and mainly interval representation) syntax and semantics. </p>\n\n<p>In <code>PostgreSQL</code> it would be</p>\n\n<pre><code>SELECT * FROM messages WHERE messagetime &gt;= messagetime - interval '1 day'\n</code></pre>\n"}, {'answer_id': 59470, 'author': 'Graeme Perrow', 'author_id': 1821, 'author_profile': 'https://Stackoverflow.com/users/1821', 'pm_score': 1, 'selected': False, 'text': '<p>For Sybase SQL Anywhere:</p>\n\n<pre><code>Select * From Messages Where MessageTime &gt; dateadd( day, -1, now() )\n</code></pre>\n'}, {'answer_id': 59508, 'author': 'hamishmcn', 'author_id': 3590, 'author_profile': 'https://Stackoverflow.com/users/3590', 'pm_score': 1, 'selected': False, 'text': '<p>For Oracle </p>\n\n<pre><code>SELECT * FROM messages WHERE messageTime &gt; SYSDATE - 1\n</code></pre>\n\n<p>(The psuedo variable SYSDATE includes the time, so sysdate -1 will give you the last 24 hrs)</p>\n'}, {'answer_id': 59532, 'author': 'John Meagher', 'author_id': 3535, 'author_profile': 'https://Stackoverflow.com/users/3535', 'pm_score': 0, 'selected': False, 'text': '<p>If you are accessing this from an API based client (I\'m guessing that is the case because of the \'?\'s in the query) you can do this from your program rather than through SQL.</p>\n\n<p>Note: The rest is for JDBC syntax, other APIs/languages will be different syntax, but should be conceptually the same.  </p>\n\n<p>On the insert side do</p>\n\n<pre><code>PreparedStatement stmt = connection.prepareStatement( \n    "INSERT INTO messages " +\n    "(typeId, messageTime, stationId, message) VALUES " +\n    "(?, ?, ?, ?)" );\nstmt.setInt(1, typeId);\nstmt.setDate(2, new java.sql.Date(System.currentTimeMillis()));\nstmt.setInt(3, stationId);\nstmt.setString(4, message);\n</code></pre>\n\n<p>On the query side do:</p>\n\n<pre><code>PrepatedStatement stmt = connection.prepareStatement(\n   "SELECT typeId, messageTime, stationId, message " +\n   "from messages where messageTime &lt; ?");\nlong yesterday = System.currentTimeMillis() - 86400000; // 86400 sec/day\nstmt.setDate(1,new java.sql.Date(yesterday));\n</code></pre>\n\n<p>That should work in a portable manner.  </p>\n'}]
['https://Stackoverflow.com/questions/59425', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1288/']