PatPHP Posted July 14, 2008 Share Posted July 14, 2008 Hi Guys I have two fields in the database a StartDate and a EndDate. Now how can I query so that I get all the records for today which are in this range? Greetings Patrick Quote Link to comment Share on other sites More sharing options...
.josh Posted July 14, 2008 Share Posted July 14, 2008 SELECT * FROM table WHERE NOW() BETWEEN StartDate AND EndDate Quote Link to comment Share on other sites More sharing options...
PatPHP Posted July 14, 2008 Author Share Posted July 14, 2008 Hey thx.... too easy. Quote Link to comment Share on other sites More sharing options...
fenway Posted July 14, 2008 Share Posted July 14, 2008 SELECT * FROM table WHERE NOW() BETWEEN StartDate AND EndDate Excep this won't utilize any index on start/end..... Quote Link to comment Share on other sites More sharing options...
.josh Posted July 14, 2008 Share Posted July 14, 2008 Ha well I readily admit I suck at sql. Can you further explain? Quote Link to comment Share on other sites More sharing options...
fenway Posted July 14, 2008 Share Posted July 14, 2008 To utilize an index, you need it in the form: SELECT * FROM table WHERE column1 BETWEEN expr1 AND expr2 So you have to move it around so one of the columns in being compared against the expressions (which can also contains column references). Sometimes this means you have to add a 2nd part to the where clause. So SELECT * FROM table WHERE startDate >= NOW() and endDate < NOW() Is "better", though it does obscure the logic a bit. check EXPAIN. Quote Link to comment Share on other sites More sharing options...
.josh Posted July 14, 2008 Share Posted July 14, 2008 huh. Well when I originally posted the string I actually wrote it exactly like that, but I thought that that was just a longer way of using BETWEEN. Quote Link to comment Share on other sites More sharing options...
fenway Posted July 14, 2008 Share Posted July 14, 2008 huh. Well when I originally posted the string I actually wrote it exactly like that, but I thought that that was just a longer way of using BETWEEN. Not always... it's something the optimizer should do, but doesn't. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.