Forum Moderators: open

Message Too Old, No Replies

How to create member joined monthly report?

         

bluebelldolphin

11:57 am on Jul 14, 2006 (gmt 0)

10+ Year Member



I create an application allowed user registered. The table name user, store user_id, username, joindate. I wanna create a function to display the member join in particular month by selecting the pull down menu-select month. Eg, user click search JULY, it will display all user join in the month of JULY. How to write the query? The joindate formate is date(d/m/Y).

coopster

4:11 pm on Jul 14, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, bluebelldolphin. First, if your column
joindate
is not of type DATE, then I would ALTER the definition and make it of type DATE. Then your query should be quite easy using your relational database temporal (date/time) operations. If that is not an option, you could use SUBSTRING to derive that portion of your format which is the month and use that in your WHERE clause comparison.

bluebelldolphin

2:42 am on Jul 15, 2006 (gmt 0)

10+ Year Member



$month = substr($row["joindate"], 3, 2);?

code below cannot work like what u mention.
$sql1 = "SELECT * FROM user WHERE MONTH(joindate) = '".$month."'";
$result1=mysql_query($sql1);

But how I implement that into the select query?

smatts9

6:50 am on Jul 15, 2006 (gmt 0)

10+ Year Member



Personally I save it separately as month, day, year, in my DB, and it is a lot easier to put the date together rather than pull it apart.

coopster

2:03 pm on Jul 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Pulling the date apart is but one way to use a stored date. There are many built-in functions for working with DATE column types that make it much easier than trying to put dates back together before using them again in query statements. Still, if your personal preference is working for you, by all means, stick with that which you are comfortable. It's merely my recommendation to keep temporal values in temporal column definitions.

bluebelldolphin, you said that the

joindate
format is date(d/m/Y). Typically, a stored date is in ISO format,
yyyy-mm-dd
. So, first things first -- you probably have not created your
joindate
column as column type DATE. You can find out by running a MySQL command that provides information about the columns in a table:
DESCRIBE user;

The "Type" column will show you the column type of that particular Field. If it is of type "char" and you have the data formatted as
dd/mm/yyyy
then you are going to get NULL values every time you try to use the MySQL built-in DATE functions. This is where I said you would need to use a SUBSTRING against your stored date string value:

$sql1 = "SELECT * FROM user WHERE SUBSTRING(joindate FROM 4 FOR 2) = '".$month."'";

If it is of type DATE, then your query will work as long as your $month variable contains the value 7, which represents the month of JULY (note: you would not need to have the quotation marks around your variable value as the returned value of a MONTH() function is an integer in the range 0 to 12).

$sql1 = 'SELECT * FROM user WHERE MONTH(joindate) = ' . $month;

The latter example is more often used and is the preferred method.