How to query a record using single date? - php

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

Related

get data between start date and end date

Get data between start date and end date
I am getting data but my requirement is little bit different pls check the below database
I have two text boxs
1) start date
2) end date
Database is like this
table name->purchase_order
sno start_date end_date
1 2017/08/01 2017/12/01
2 2017/08/01 2017/11/30
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
5 2017/10/01 2017/11/30
I am trying like this
select *
from purchase_order
where start_date>= '2017/09/01' and start_date<= '2017/09/01'
OR end_date>= '2017/09/01' and end_date<= '2017/09/01'
Output i am getting
sno start_date end_date
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
What i require
if i select between this 2017/09/01 - 2017/09/30 i want out put like this {in id "1" in between 8th month to 12th month there is 9th month is there so it also has to come}
sno start_date end_date
1 2017/08/01 2017/12/01
2 2017/08/01 2017/11/30
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
thanks
Instead of checking that the column values are between your dates, you want to see if your dates are between the column values. Using BETWEEN makes the logic look a little cleaner:
select * from purchase_order where '2017/09/01' BETWEEN start_date AND end_date
If you select 2017/09/01 - 2017/09/30, then do this:
select * from purchase_order where '2017/09/01' BETWEEN start_date AND end_date AND '2017/09/30' BETWEEN start_date AND end_date
AND will make sure both dates are between start_date and end_date, OR will make sure at least one of the dates is between the columns.
You would not want to have 4 clauses as you have in your question, you would want to have just 2.
The logic should be as follows:
the start date from the input in the application is later or equal to the start date in the start column in your table
the end date from the input in the application is earlier or equal to the end date in the end column in your
application.
Based on this logic, you should be able to use the following query:
SELECT
*
FROM purchase_order
WHERE start_date<= '2017/09/01' -- start date input
AND end_date<= '2017/09/01' -- end date input
One may complain that I have a hole in my logic due to the fact that I don't check to see that the end date is later than the start date, though you would want to do that at the application level, hence it is not relevant at the database level where this question was asked.

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 check if a day in a timetable is full or not in SQL Server

I'm finishing a small web app that allows a group of people to reserve rooms for meetings and other activities. So, I have a monthly report with the days of the chosen month. After selecting a day, It will show the scheduled timetable for the day and for that room.
The days are color coded whether they have no reservations made are partially full or are completely full.
On the SQL Server side I have a table to register the reservations made in which I have fields for the role, day (date), start time and end time of the meeting (both datetime).
So my question is: is there a way to know, with querying, if a day is completely full or not? I want to have some kind of left join or CTE to put a flag in the select statement telling me if the day is full or not.
EDIT: I was asked for the table structure so I'm putting it here to help other people who might run into the same kind of question.
Table "reservations":
id INT PRIMARY KEY,
id_type_event INT,
reservation_date DATE,
start_time TIME(5),
end_time TIME(5),
id_reservation_state INT,
date_created DATETIME,
id_user_created INT
Question 2: should I change the "start_time" and "end_time" to DATETIME?
If you have a date table then you could so something simple like this;
DECLARE #QueryDate date; SET #QueryDate = '2016-05-31'
SELECT
dt.BookingDate
,bkd.Room
,CASE
WHEN bkd.HoursBooked < 8
THEN 'Not Full'
WHEN bkd.HoursBooked > 10
THEN 'Over Booked'
ELSE 'Fully Booked'
END BookingStatus
FROM
DateTable dt
LEFT JOIN
(
SELECT
Room
,BookingDate
,SUM(DATEDIFF(mi,StartTimeDate,EndTimeDate)) HoursBooked
FROM RoomBookings
GROUP BY
Room
,BookingDate
) bkd
ON dt.Room = bkd.Room
AND dt.BookingDate = bkd.BookingDate
WHERE dt.BookingDate = #QueryDate
I'm working with limited information in your post, but if you have two tables, one being the date table the other being the bookings table then this will give you what you're after.

SELECT month day year as date in mysql?

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');

how to show one record per day order by id?

I have this little script that shows one wisdom each day.
so I have three columns.
Id wisdom timestamp
1 wisdon 1 4/1/2012
2 wisdon 2 4/1/2012
3 wisdon 3 4/2/2012
and I want to fetch array of one wisdom for each day
I looked around your website, but unfortunately I didn't find something similar to what I want.
also I got this code
$sql = mysql_query("SELECT DISTINCT id FROM day_table group by timestamp");
but this also not working.
any ideas?
is it possible to make a counter of 24 hours update wisdom date?
please give me some help.
You can make another table that is called wisdom_of_day
The table would have the following columns, id, wisdom_id, date
Basically each day you can randomly select a wisdom from your wisdom table and insert it into the wisdom day table. You can also add a constraint to your date column so it is distinct. It is important that it is a date column and not a timestamp since you don't care about time.
Then you can retrieve the wisdom of the day by querying based on the date.
It's possible I read your question wrong and you just want to select one wisdom for each day, but you want to show multiple days and you want to get the data from your table.
If so, the reason your query is not working is because you are grouping by a timestamp which includes the date and time. You need to group it by date for it to group like you want.
Here is a query that will group by the day correctly. This will only work if you have a timestamp field and are not storing a unix timstamp on an int column.
select id, wisdom, date(timestamp) date_only from day_table group by date_only order by date_only asc;
Hmm, I noticed that your timestamp values are in some kind of date format, maybe as a string? If so the above query probably won't work.
First compute number of days since 1970
SELECT DATEDIFF(CURDATE(), '1970-01-01')
Then insert this number inside RAND, for example:
SELECT * FROM table ORDER BY RAND(15767) LIMIT 1;
Rand with number as argument is deterministic.
Full query:
SELECT * FROM table ORDER BY RAND((SELECT DATEDIFF(CURDATE(), '1970-01-01'))) LIMIT 1;

Categories