SELECT month day year as date in mysql? - php

Using a mysql database, I want to select only ContentObject ids from today forward (content objects associated with current and future events). But I'm not sure how to select the month, day, and year from the Events table as a date to compare with today's date. Can someone show me how to do this? Thanks!
I'm going to be using this sql from a php script, so if it's easier to do some work in php, that's fine, too.
Schema:
Events Table:
id |int(11)
attribute_id |int(11)
month |tinyint(4)
day |tinyint(4)
year |int(11)
Attributes Table:
id |int(11)
content_id |int(11)
ContentObject Table:
id |int(11)
Figured it out: This got me what I was looking for:
select E.id, E.year, E.month, E.day
from Events as E
where (E.year*10000 + E.month*100 + E.day) >= (CURDATE() + 0)

The best way is to add date filters into the SQL treatments...
In SQL you have a lot of date function
See MySQL doc here to retrieve month, year, or whatever you wan't...
Example for months :
SELECT * FROM mytable WHERE column_month = DATE_FORMAT(CURRENT_DATE, '%c');

Related

how to fetch exact data from mysql table in month date condition

hello everyone for my erp system i want some specific type of data fetch from database
for example if i want number of row added in current month purchase then i use
select count(receive_no) FROM tbl where contact_id=".$contactid." AND MONTH(created_on) = MONTH(CURDATE());
but it gets false results
and if i want sum of result in current month then i use
select sum(price) FROM tbl where contact_id=".$contactid." AND MONTH(created_on) = MONTH(CURDATE())
and if i want today date number of row or purchase then i use
select count(receive_no) FROM tbl where contact_id=".$contactid." AND created_on >= CURDATE()
so as if i want today sum of purchase then i use
select sum(price) FROM tbl where contact_id=".$contactid." AND created_on >= CURDATE()
my table created on field datetime like 2016-09-01 11:56:45
so please suggest me better query to fetch exact data
thanks
It's not really clear what - if any - the problem is with the last 3 queries, but the first one will get you the data from this month this year and this month last year, the year before that, etc. etc.
So what you need is something like (there are several ways to write that...):
select count(receive_no) FROM tbl where
contact_id=".$contactid."
AND MONTH(created_on) = MONTH(CURDATE())
AND YEAR(created_on) = YEAR(CURDATE())

How to query a record using single date?

Example: Record A has start date field (12/11/2013) and end date field (14/11/2014). How to get record A if I query using a single date (13/11/2014) query as given.
Record Start_date End_date
A 12/11/2014 14/11/2014
This is for booking facilities system. If a user books a notebook from 12 till 14/11/2014, other users should not be able to book the same notebook on either 12th, 13th or 14th. Using the select statement with the single date, example 13/11/2014, how can we show to the user that the notebook has been booked?
select * from your_table
where '2014-11-13' between start_date and end_date
SQLFiddle
After seeing Juergen d's answer I felt the query was right but after reading the para below the table I felt he needs something like this
Select case when '2014-11-13' between start_date and end_date then 'Booked' else 'Open' end case from your_table

MySql - SUM of workers per hour

With a little (lots) help from StackOverflow last year, I have a function that will show how many employees that are currently at work.
The code works fine - but now we have implemented night shifts and the original query only shows shifts that begin in the current date. People working nightshifts might check-in at 10PM and the leave at 8AM - in the current query, they don't show up, because startdate is the day before...
I have been trying to implement enddate in the query, but with no luck.
teamslots
---------
id
startdate
starttime
enddate
endtime
teamslot_schedule
-----------------
id
slotid (joins to is in teamslots)
userid
shifthours
----------
thehour
This is the original query - I could really use some help that would make the query include employees, that are on a shift that begins the day before, but ends "today".
SELECT
DATE_FORMAT(d.startdate + INTERVAL s.thehour HOUR, '%Y-%m-%d %H') AS date,
COUNT(DISTINCT ts.userid) AS users
FROM
shifthours s
JOIN
(SELECT DISTINCT startdate FROM teamslots) d
LEFT JOIN
teamslots t ON t.startdate = d.startdate AND
s.thehour BETWEEN HOUR(t.starttime) AND HOUR(t.endtime)
LEFT JOIN
teamslot_schedule ts ON ts.slotid = t.id
GROUP BY
d.startdate,
s.thehour
ORDER BY
d.startdate + INTERVAL s.thehour HOUR;
Best regards,
Mark
Use the MySQL CURDATE() function and subtract the appropriate interval for your shift length.

What mysql code do I need to use? to find all ids made between date

I'm trying to work out the amount of ids between two dates basically I want to find out how many users I have gotten today so im not sure what I need for it really :/
I have a column call date which has the users date of registration
And I have the ID column of course which will help
Please help
Use BETWEEN. For example:
SELECT
COUNT(*) AS total_users
FROM
users
WHERE
date_of_registration BETWEEN CURRENT_DATE AND INTERVAL CURRENT_DATE - 1 WEEK
SELECT
id
FROM
t1
WHERE
DATE(`date`) = CURDATE()
The DATE() call is onlyi necessary of date is a timestamp.

Get Current year and next form mysql

Im am selecting various things from a table. The problem is I only want to select them if they belong to the current year and the next year.
My table looks like this
Title Text Date
The date is formated like this 0000-00-00 and is in the format "date"
So the question is how can i select only items from only this year and the next?
Example: the year is 2012, I have items in my table that is old and i dont want them to show - I only want to select items from at the first 2012 1 January and last in this case 31 Dec 2013 current year 2012 + 1 year.
Thanks a lot for any help!
SELECT
*
FROM
table
WHERE
YEAR(date) = YEAR(CURDATE())
OR
YEAR(date) = YEAR(CURDATE()) + 1
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
SELECT
*
FROM
table
WHERE
date BETWEEN CONCAT(YEAR(CURDATE()),'-01-01') AND CONCAT(YEAR(CURDATE())+1,'-12-31')
As ugly as it looks, it allows your query to use index on date field.
Better idea is to create limiting dates in external PHP script, so that the query can use query cache.
If you only want to show items, that are no older than two years, you can do this:
SELECT
*
FROM
table
WHERE
date >= CURDATE() - INTERVAL 2 YEAR;

Categories