Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show data in current day, week, month and year

Tags:

php

mysql

I have table user, it contains id_user, username and user_time. And user_time is the time a user registered, the properties is timestamp so the output is like this:

User A | user_time > 2014-12-22 10:10:10

Then I use this query to display data per year

SELECT * FROM user WHERE user_time = YEAR(NOW());

The query works but just returns an empty row. It's not only the year, but also day, week, and month return empty rows. Why is this query not returning any data?

like image 991
Andhika R.K. Avatar asked Dec 29 '14 09:12

Andhika R.K.


People also ask

How do I get Excel to show data this week?

Click a cell in the date column of the pivot table that Excel created in the spreadsheet. Right-click and select "Group," then "Days." Enter "7" in the "Number of days" box to group by week. Click "OK" and verify that you have correctly converted daily data to weekly data.

How do I filter data by day of the week in Excel?

To filter weekdays or weekend days, you apply Excel's filter to your table (Data tab > Filter) and select either "Workday" or "Weekend".

How do I use conditional formatting in Excel today's date?

Excel conditional formatting for dates (built-in rules) Microsoft Excel provides 10 options to format selected cells based on the current date. To apply the formatting, you simply go to the Home tab > Conditional Formatting > Highlight Cell Rules and select A Date Occurring.


1 Answers

Problem is that you doesn't do anything with your data:

SELECT * FROM user WHERE user_time=YEAR(NOW())

Try to get the YEAR from the user_time too, like this, so you do compare the numbers, not timestamp and a number:

SELECT * FROM user WHERE YEAR(user_time) = YEAR(NOW())

Or, as @ItsMe suggested:

EXPLAIN SELECT * FROM user WHERE YEAR(user_time) = YEAR(NOW())

And please, avoid the * in your queries, this is a bad practice.

like image 155
VMAtm Avatar answered Sep 18 '22 08:09

VMAtm