简体   繁体   中英

mysql how to get the type of data in a varchar field using query. as it can hold any datatype as string

The reason for a second sentence in my question " as it can hold any datatype as string." is to make the question more specific from the view of it so that people will really get the actual picture before going to the answers.

i have asked this question after posting and getting responses to this question . according to those responses i have planned to do it this way. i am just mentioning it here and if you have time then you can go through. because this current question has been raised after that.

the current quesiton is as follows...

  1. I am having a field with varchar datatype.
  2. by default it is null.
  3. it will hold either null or string(which is the filename of an image) or the timestamp value of the time when that image was uploaded.
  4. If it is null or if it is the integer value of timestamp value then i want display no-image.jpg.
  5. if it is a string then it is the actual image so i will display that image.
  6. if the field holds null datatype then no image has been uploaded.
  7. if it is a timestamp value then the users is still manipulating his uploaded image.
  8. if it is a string then that is the actual image which is ready to be displayed.
  9. uploads are done by only one person.

so, i am executing a query and in that i want to find whether the varchar value is null or integer or a string.

i dont want to do more calculation in php. i hope there should be an option in sql to do that.

if you feel you would like to give both the answers (by solving it in sql or by solving it in php) then that is too fine.

here is what i have planned to do in php.

$val = $rs['filename'];
if(is_null($val)) // display a noimage.jpg
else if(is_numeric($val)) // display a noimage.jpg
else // display the actual image.

what i expect in sql is to call a function and if null or integer then i will use the as keyword and 'noimage.jpg' else as the actual image...

select somefunction(filename) as something.... the something should hold noimage.jpg if the value is null or integer. if the value is a string then it should hold the actualimagename.

hope i have made it clear.

Why not add another column to the table to specify what this field is? Not to say this would make what sounds like a very bad design any better, but it would be safer than trying to figure out what you stored in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM