Menu
I wanted to know how you search from a datestamp when you are only looking for a specific year?
i.e. 2008 I want to search my database for the all records in “date” containing 2008 that has the datestamp format YYYY-MM-DD?
Would this same format work for select count and fetch records?
Thanks in advance.
[code=php]$query = "select * from table where date_field between date('2008-01-01') and date('2008-12-31')";
$result = mysql_query($query);
// ... etc.[/code]
[CODE]
//connect to database
$dbconn = mysql_connect ( $dbhost, $dbuser, $dbpass );
mysql_select_db( $dbname, $dbconn );
//count records
$tte = mysql_query("SELECT COUNT(*) FROM _cpof_fallen_officers
WHERE date
between date('2008-01-01') and date('2008-12-31')", $dbconn);
$tte_count = mysql_result($tte, 0);
[/CODE]
[code=php]$tte = mysql_query("SELECT COUNT(*) FROM _cpof_fallen_officers
WHERE date
between date('2008-01-01') and date('2008-12-31')", $dbconn);
if ($tte) {
$tte_count = mysql_result($tte, 0);
} else {
print "<pre>mysql_error: " . mysql_error() . "</pre>n";
}[/code]
date
<i>
</i>SELECT . . . WHERE YEAR(<span><code>date</code></span>) = 2008
I'm under the impression that using the YEAR() function on a field will force MySQL to bypass using any index(es) on the date[time] field. I could be mistaken.
Of course, if the field isn't indexed, I suppose it doesn't really matter ...[/QUOTE]
[CODE]
$tte = mysql_query("SELECT COUNT(*) FROM _cpof_fallen_officers
WHERE date(date
) between '2008-01-01' and '2008-12-31' ", $dbconn);
$tte_count = mysql_result($tte, 0);
[/CODE]
date
The DATE() function is used to extract the date part of a timestamp. As such, you don't need in BETWEEN...AND but you should use to extract the date part from the field.[/QUOTE] date
For best results when using BETWEEN with date or time values, you should use CAST() to explicitly convert the values to the desired data type. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE. (http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between )[/QUOTE]
[CODE]SELECT COUNT(*) FROM _cpof_fallen_officers
WHERE date
between timestamp('2008-01-01 00-00-00') and timestamp('2008-12-31 23-59-59')[/CODE]
[CODE]
$tte = mysql_query("SELECT COUNT(*) FROM _cpof_fallen_officers
WHERE date
LIKE '2008-%' ", $dbconn);
$tte_count = mysql_result($tte, 0);
[/CODE]
using date() to extract a date part from two literals which consisted of only date parts was useless.[/QUOTE]
In "real life" terms, however, if all I'm insterested in is the year, the fastest and easiest way to go would be:
[CODE]$tte = mysql_query("SELECT COUNT(*) FROM
[/QUOTE]_cpof_fallen_officers
WHEREdate
LIKE '2008-%' ", $dbconn);
$tte_count = mysql_result($tte, 0);[/CODE]
date
date
Forget the theory... Just try it.[/QUOTE]
And, as far as I know, internally, MySQL date fields are stored as text ('YYYY-MM-DD HH:MM:SS'). Perhaps that's why it's faster? [/QUOTE]
You'll see how much faster it is. Above all, it is less error-prone than all this casting and juggling the date.It's possible you're query is better, at least for the version of MySQL you're running it on. However, all the data I have suggests there's another reason your query would appear to be outperforming the more theoretically efficient one, especially since the theoretically more efficient one is also more efficient in practice on my installation and schema.
[/QUOTE]
0.1.9 — BETA 6.17