views:

38

answers:

1

lets say I have a "Datetime" column in DB. It has a data like "2010-10-01 09:12:00". Beside that, i want to make a searching page which have:

<select id="month" name="month">
       <option value="01">01</option>
       <option value="02">02</option>
       ...
</select>
<input type="text" id="year" name="year">

How do i do to get data if i choose month=10 and year=2010?

+3  A: 
SELECT * 
FROM   your_table 
WHERE  MONTH(your_date) = '10' AND YEAR(your_date) = '2010';

Be aware that the above won't be able to use an index on your date column, if one exists. To use an index, you'd have to use something like this:

SELECT * 
FROM   your_table 
WHERE  your_date >= STR_TO_DATE(CONCAT('2010', '-', '10', '-01'), '%Y-%m-%d') AND
       your_date < STR_TO_DATE(CONCAT('2010', '-', '10', '-01'), '%Y-%m-%d') + INTERVAL 1 MONTH;
Daniel Vassallo
to use index he need to use a date between query
Haim Evgi
whats the meaning of "your_date"? is that the column's name?
klox
@klox: Substitute `your_date` with the name of your `datetime` column.
Daniel Vassallo
@Daniel: how to send combobox and textfeld value into this query? how to use POST or postVar corrrectly? are we need use concat?
klox
@klox: Your HTML is fine. Simply wrap that `<select>` field into a form, and post it to your php script. Then simply replace the `'10'` and `'2010'` constants in my example with the data posted from the HTML. Make sure you do not allow an [SQL Injection](http://stackoverflow.com/search?q=sql+injection) vulnerability.
Daniel Vassallo
@Daniel:ya..I know that,but how if i want choose another month and year?
klox
@klox: Simply replace the `'10'` and `'2010'` in my examples with the values posted from your form... Or am I misunderstanding you?
Daniel Vassallo
OK I understand about your answer. But it can be used if I only specify one option . my question here is what if I choose another option on the combobox and fill out another year on the textfield, of course we have to use post or get,isn't it? well I'm just confused on the placement of the post or get itself.
klox
@klox: Did you try it out? It looks like you're on the good track, but you may want to update the question with your attempt, or maybe post another one if the update is not that related to the original question.
Daniel Vassallo
@Daniel: i edit my question.
klox