I want to select an entry from my database randomly, but not for every call, but depending on some other external factor like week of the year or day of the year. How can i achieve this?
This will tell me the current week:
SELECT DATE_FORMAT(CURDATE(),'%u');
So, to get a record based on this value, I select the record number:
SELECT (DATE_FORMAT(CURDATE(),'%u')+0) MOD (SELECT COUNT(*) FROM my_view ORDER BY id);
So my attempt:
SELECT * FROM my_view LIMIT (SELECT (SELECT DATE_FORMAT(CURDATE(),'%u')+0) MOD (SELECT COUNT(*) FROM my_view ORDER BY id)) ,1;
And all I get is:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT (SELECT DATE_FORMAT( CURDATE(),'%u')+0) MOD (SELECT COUNT(*) FROM ' at line 1
Maybe this is not the best approach? Is it impossible? Thanks.
UPDATE: I want to insert this into VIEW, so I can only use SELECT statements. Anyways, I already solved this, please see this comment: Weekly ORDER BY on MySQL
source share