Question:
mysql select query_how to apply if else condition?
karthik
2010-03-06 11:02:02 UTC
i have some records on my mysql db. my table name is 'facts' and the particular field name is 'factdate'. In the factdate field has some records of dates. They are like, 1947-08-15 or 1235-00-00 or 0000-04-14 or 1984-00-17 or 0000-00-10 or 0000-07-00 or sometime its null. So, how to i retrieve records on the basis of user select any one of the method like year month day only or year only or month only or day only or year and month only or etc. so, however like anyone. So, i think the select query used by the if else condition, so how to apply this. Please give a sample query for this problem.

Note: the factdate field data types is ' varchar '.
Five answers:
anonymous
2010-03-06 12:42:19 UTC
It's going to be very difficult (which means slow) doing it that way. If you want a selection on, say, just the month, but of any year, I'd use 2 additional fields, month and day. (Year can be "WHERE factdate LIKE '%';") If the user enters a month or a day, search WHERE the month or day field = the user input. (If the user enters a single digit, add a '0' in front.)
plyler
2016-10-04 07:35:35 UTC
a million. you may in basic terms subtract it as quickly as, then it is going to become 20. 2. I have no concept! 3. back, no concept... am i able to get 4 factors for quantity a million? EDIT: How is quantity 2 a river? Rivers don't have mouths? EDIT: i could have under no circumstances gotten quantity 3, is there any way i'm getting get 8 factors and we are able to call it a deal? nicole
Red
2010-03-06 11:14:29 UTC
Use the WHERE and REGEX for best match results.



By Year:

$query = "SELECT * FROM `facts` WHERE `factdate` REGEXP \"^1947-..-..$\""



By Day:

$query = "SELECT * FROM `facts` WHERE `factdate` REGEXP \"^....-..-15$\""



By Month:

$query = "SELECT * FROM `facts` WHERE `factdate` REGEXP \"^....-06-..$\""



My regex is a bit sloppy, there are better patterns to use, but this should get the job done.
arvind
2010-03-06 11:08:02 UTC
if u are using de varchar type den i would suggest u to use de like command ask de user to enter any data say de year 1947 u can use year like"%1947%....something like this should solve your problem i guess
Vikas G
2010-03-06 11:13:11 UTC
Try using

select to_char(column_name,'mm-dd-yyyy') from table_name;



This should work... U could change the format types to whatever format type u ve wanted.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...