Currently I have two tables and the following sql statement which correctly retrieves the items in the events table ordered by their dates in the event_dates table:
SELECT * FROM events, event_dates
WHERE events.id=event_dates.event_id
AND events.preview=0 AND event_dates.start_date>=now()
ORDER BY event_dates.start_date ASC,event_dates.start_time ASC LIMIT 3
Now I want to add an extra AND to make sure only the events on the next weekend are set. The date column is in a standard mysql date format (YYYY-MM-DD). Got stuck on this bit. Cheers.
Use PHP strtotime() to get the start and end timestamp of the weekend:
$we_start=strtotime('next saturday');
$we_end=strtotime('next monday')-1;
Then do a sql query to search for timestamps BETWEEN them.
select * from mytable where UNIX_TIMESTAMP(mydatefield) BETWEEN $we_start AND $we_end
Hope that helps.
Related
I have a problem related to MySQL query, I use WAMPServer.
I have data in database which have range of dates but when I select data for example
select * from CHD WHERE addtime>='2018-06-15' and addtime<='2018-06-21';
It displays data from '2018-06-15' to '2018-06-20', data of 2018-06-21 are not displayed even if I do
select * from CHD where addtime='2018-06-21';
is not working
Please anyone can help me
This assumes that your column is of type datetime.
The shorthand version of your date in the filter clause is assumed to be at midnight of the date. Your values that you are attempting to retrieve have times after midnight of that date. You either need to define a timestamp along with the date, or you need to filter by the day after for less than equal to or the day before for greater than equal
I have a database with a bunch of rows. One of the columns is "date" which has a date for each row in the format: MM/DD/YYYY H:MM PM/AM
How can I write PHP that selects the row where the date is the most recent date that has passed. Meaning if you have a date of tomorrow, a date of today, and a date of yesterday, it picks the row where the date is of yesterday.
I've already connected to the database:
mysql_connect('localhost','username','pass');
mysql_select_db('db_name');
How do I write a query to grab this?
I would do the filtering in SQL rather than PHP, using a variation of the following query:
SELECT *
FROM myTable
WHERE theDate < CURDATE()
ORDER BY theDate DESC
LIMIT 1
This selects all the rows in the past (theDate < CURDATE()), sorts them in reverse chronological order (ORDER BY theDate DESC), then takes the first record (LIMIT 1).
When you query the database, ORDER BY date DESC LIMIT 1
This will only return the most recent result, and thus the result that is closest to the current date. (This only works if you don't put entries that are dated in the future)
I have a database of events that are used to fill a PHP calender. I want to also have a page that lists three upcoming events in date order when ever the page loads. The records have a separate field for day, month and year. The issue i run in to is getting records that accrue after the current date.
Table structure will be good to have but I will guess the query that you need. Use that as a point to start from.
SELECT * FROM events WHERE CONCAT(year,'-',month,'-',date) > DATE_FORMAT(NOW(),'%Y-%m-%d')
ORDER BY year DESC, month DESC, date DESC LIMIT 3;
You are going to have to build a date from your three fields to test against current date, or split current date into year, month, day and test all three against data in your table.
Got to say, not a brill decision on someones part to split date like that.
Need table structure before we can properly help query wise.
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;
I have a list of dates in a table in a MySQL database (the dates when a charity bookstall is to be held), which I want to display on a page. On one page I'm displaying the date of the next stall, and on another the dates of the stall in the next month. (Currently I'm using an unordered HTML list and selecting the dates with PHP, but it's a bit messy, and I also want to tie in the dates with the fundraising totals that are stored in the database).
I want to put the dates in a database though so that I can tie in the dates with the fundraising totals for each week. I'm thinking that once I can identify the date with the nearest up-coming date that I can use 'LIMIT 1' to select the next week's date for display, and 'LIMIT 4' say for where I need to display the dates for the next month, but what I can't figure out is how to identify the record with the nearest up-coming date - identifying the current date and then selecting the nearest date...I have a feeling there's probably one of the MySQL date functions that can be persuaded to help out in this, but can't figure out exactly how.
Any ideas on how I can do this?
If I understand correctly, you can just pick up next four dates that are after today.
In MySQL you could use the CURDATE() function for the 'today' bit, then apply an order and limit to your select statement. For example,
SELECT stall_date
FROM stall_dates
WHERE stall_date >= CURDATE() -- >= assumes you want today's to show too
ORDER BY
stall_date
LIMIT 4
Use ORDER BY stall_date DESC to reverse the ordering if needed.
If your column is a DATETIME field, you can identify the next by using SELECT...WHERE event_date > "2009-11-06" and ORDER BY event_date.
SELECT * FROM so_events
WHERE event_date > "2009-11-06 15:36:00"
ORDER BY event_date ASC
LIMIT 4
MySQL will internally do the work for you and select rows where whose timestamp is greater than the one you specify in the WHERE clause.