-1

Problem: I want to select row in MySql based on user supplied month value, checking should go through all the records and check each date coloum by month. I have a table lets say

id int(4), date date 

Now I want to retrieve this data using select statement, in the select statement i want to check the date by month not by the whole date because user has suppllied value in month ! whole date.

For example I want to make this kind of query:

select * 
  from `Table` 
 where id=123 
   And date = "value in month"

Many thanks.

4

2 回答 2

0

使用MONTH零件。

SELECT ...
WHERE MONTH(date) = 5
于 2013-05-21T02:10:45.880 回答
0

尝试

SELECT * 
  FROM Table1 
 WHERE id = 123 
   AND MONTH(date) = 3

如果你也关心一年

SELECT * 
  FROM Table1 
 WHERE id = 123 
   AND MONTH(date) = 3 -- March
   AND YEAR(date)  = 2013

SQLFiddle(对于两个查询)

请注意,date即使存在列,也不会在列上使用任何索引。

于 2013-05-21T02:10:48.113 回答