If it is h开发者_运维问答ow can i query on double and date, I mean there has to be a way to cast them back and them sort them for example.
No, it is not okay. You should use the appropriate data types. According to the MySQL Reference Manual, for storing dates and/or times, you have available to you DATETIME, DATE, TIMESTAMP, TIME, and YEAR. For doubles, there are various numeric types.
Storing doubles and dates into VARCHAR fields can only get you into trouble, and cause performance problems in addition.
Sure, this can be done, but why do you need to do that, when data types exist that do it for you more reliably and with better performance?
I think I smell the EAV antipattern here...
But to answer your question, you can cast a string containing numbers to the actual numeric value like this:
SELECT '123.456' + 0.0;
You can cast a string containing a date literal to the actual date data type with the STR_TO_DATE()
function:
SELECT STR_TO_DATE('Nov 12, 2009', '%M %d,%Y');
精彩评论