English 中文(简体)
仅使用今天的日子和月的MySQL数据库检索记录
原标题:Retrieve records from MySQL database using only today s Day and Month in Symfony2

我在网站上建造一个服务,根据人们的视线展示历史事件。 因此,如果你于1月1日上台,那就只能显示当天发生的事件。

目前使用的是Im。

    $em = $this->getDoctrine()->getEntityManager();

    $onthisday = $em->createQuery( 
            SELECT otd.thedate, otd.title, otd.content
            FROM ShoutMainBundle:Onthisday otd
            WHERE otd.thedate = CURRENT_DATE()
            ORDER BY otd.id DESC 
        );

    $otd = $onthisday->getResult();

问题在于,由于它使用<代码>CURRENT_DATE(),它只会带来与日、月和年相匹配的回报结果。

我想要做的是,只能用今天和月的时间取回记录,而忽略一年。

我如何能够做到这一点?

EDIT: I ve now used this code:

WHERE MONTH(birthday) = MONTH(CURRENT_DATE)
AND DAY(birthday) = DAY(CURRENT_DATE)

造成以下错误:

[Syntax Error] line 0, col 100: Error: Expected known function, got DAY

最佳回答

视贵会议桌位大小,从这个胜数的当天和月间过滤速度非常快,因为没有能够很好地满足问题的指数。

你可以做的是使用“Date Dimension”表格,并根据你的数据日期加入。 然后,如果你在当日和月份反对这一表格,你将每年从内容表内恢复一个单一记录,然后将并入你的主要数据表,并用效率较高的人收集你所需要的所有信息。

See my answer to the following question for an explanation of the date dimension table...

在特定日期内选择所有月,包括价值0的月

希望:

问题回答

使用<代码>createNative Query代替create Query

http://www.doctrine-project.org/blog/doctrine2-native-queries

您可使用MySQL <> MONTH()职能,仅从规定日期起抽出日期和月份部分。

WHERE DAY(otd.thedate) = DAY(CURRENT_DATE())
    AND MONTH(otd.thedate) = MONTH(CURRENT_DATE())

职能清单见:rel=“nofollow”>ts





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签