I have two fields, "cont_time_published" and "cont_date_published" that I want to use to filter results in a listing of records.
select *
from news
WHERE cont_date_published < CURDATE() AND cont_time_published < CURTIME()
I am trying to achieve listing that only shows records that the publish time and publish date is in the past. So that it would filter a record that has today's date but the time is still in the future.
Results are wrong when the date is today and the time is future.
cont_date_published is "DATE" only field and cont_time_published is "TIME" Field.
If you need to get the records published in the past (today in the past hours or before) try:
SELECT *
FROM news
WHERE cont_date_published < CURDATE()
OR (cont_date_published = CURDATE() AND cont_time_published < CURTIME());
sorry my mistake, forgot the OR
Try this
SELECT *
FROM news
WHERE (cont_date_published < CURDATE() ) AND ( cont_time_published < CURTIME() )
ORDER BY id DESC;
You can CAST to an actual date:
WHERE CAST(CONCAT(cont_date_published, ' ', cont_time_published) AS DATETIME)<NOW()
... but this will possibly prevent query optimiser from using indexes (if any). An alternative would be:
WHERE cont_date_published<CURDATE() OR
(cont_date_published=CURDATE() AND cont_time_published<CURTIME())
Of course, all this extra work could be easily avoided with a proper database design that makes use of a single DATETIME column:
WHERE cont_published<NOW()
select * from news WHERE cont_date_published < CURDATE()
This will be enough to get the records that are Published in the Past.
Related
I am just learning MySql (SQL in general) and I have a question. I ran a process to populate a table with 72 records. This was done, however, I needed to run the process again and this time it populated the table again with a second record for each user for a total now of 144 records. How can I isolate the newest records created today?
A simple solution is to use current_date to figure out today's date and date() to remove the time portion of your column. Then:
where current_date = date(createdTS)
This is fine for a small dataset as yours. As general solution, you'd need a query that won't need to manipulate every row, e.g.
where createdTS >= current_date and createdTS < current_date + interval 1 day
You just have to use your createdTS column, (assuming you know what was the timestamp of both runs).
SELECT * FROM `my_table` WHERE `createdTS` > '2019-07-25 15:00:00'
You could also RANK() over and get only the newest run for each user (something like this)
im having a problem where i cant think of a solution, maybe im having a bad table-structure or i just dont know enough about mysql select commands to think of a good solution. Maybe you can help me out:
So i got a table that has a Column with the Date-format (yyyy-mm-dd) i wanted to select all upcoming dates so i did:
SELECT * WHERE date >= now.
This worked kinda well but i also got "dates" where only the year is entered (2014-00-00) i also wanted to select these but "now" is already bigger so i made another column with the year only and if the month, date or both arent known i will use 0000-00-00 and the Column "year" now i could select like this:
SELECT * WHERE date >= now AND year >=now(year)
Now all entrys with 0000-00-00 wont be selected. If i use OR the entrys from last year will be shown.
So thats my problem, is there any way i can change my table so i can have entries with only the year or only year and month and of course all together? I already considered get rid of the date-format and use simple INT with seperated columns for year, month and date. But i think i will have the same problem.
Sometimes i just want to do a capsuled select like
SELECT *
WHERE (date >= now AND year >= now(year))
OR date == "0000-00-00" (i know that this doesnt work)
If I understood your problem correctly, you could use this request:
WHERE (date >= now OR year > now(year))
There is probably a simpler way though, that would preserve your design, like initializing at January 1st (01-01) instead of 00-00
I think you can use this code:
$_SESSION['month'] = //set here your selected month
$_SESSION['year'] = //set here your selected year
SELECT * FROM table WHERE DATEPART(m,date) >= '".$_SESSION['month']."' AND DATEPART(yyyy,year) >= '".$_SESSION['year']."' AND date <> '0000-00-00'
Change your table structure format. Actually just allow for that field to have null value when not entered. By default it will be null then. You shouldn't be storing 0000-00-00 as a value for Date type field. I would rather leave it as null , or as suggested in some of previous answers, initialize it with some other date. It would be much easier to manipulate with database then.
the problem is that half of you write is not MySQL and your database schema is terrible...
You have the following problems:
column data date does not have the date data type.
To fix it, you need to add a cast to the select statement eg. cast(datecolumn as date)
select * from table where cast(datecolumn as date) >= '2014-01-10';
the way to use now date is using the now function.
select now(), date(now());
result> 2014-01-10 11:11:36, 2014-01-10
select * from table where cast(datecolumn as date) >= date(now());
Because your datecolumn is not a date (2014-00-00 is not a valid date), you need to use string manipulation to extract the year.
select substring('2014-01-01', 1,4)
result> 2014
select * from table where substring(datecolumn, 1,4) = year(now());
The comparassion operator is = and not ==
the select statement syntax looks like this (pay attention because you are missing the table in your statement)
select * from [Table] where [column] = condition ...
You probably need or instead of ands, therefore your query should look like this:
select * from FooTable where
cast(datecolumn as date) >= date(now())
or substring(datecolumn, 1,4) >= year(now())
or datecolumn = '0000-00-00'
You should use something like phpmyAdmin or mySQL workbench to test your sql queries before try to use them on php, java or whatever is your programing language.
I have a table that stores seasonal address dates:
addressStartDate, addressEndDate
I'm trying to find people whose seasonal addresses were historically active in May, but I'm not quite sure how to format it. I just can't seem to conceptualize how to build the query based on the two date fields. The "was active" part is the part I'm having issues with.
The years for the dates in this case are all stored as 1000, since the seasonal address switch happens at the same time every year. So what I'm looking for is people whose addresses were active between DATE '1000-05-01' and DATE '1000-05-31' - I hope this suffices to describe my issue
Try this
SELECT *
FROM yourtable
WHERE addressStartDate >= '1000-05-01'
AND addressEndDate <= '1000-05-30'
Perhaps:
SELECT *
FROM mytable
WHERE addressStartDate <= ? AND addressEndDate <= ?
Are you looking for something like this:
SELECT addressStartDate, addressEndDate, otherfields
FROM yourTable
WHERE addressStartDate >= '1000-05-01'
AND addressEndDate < '1000-06-01'
Or depending on the interpretation of your question, if an address date just needs to exist in the month, perhaps something like this:
SELECT addressStartDate, addressEndDate, otherfields
FROM yourTable
WHERE addressStartDate < '1000-06-01'
AND addressEndDate >= '1000-05-01'
Depending on how you're storing your data (with or without time), instead of using BETWEEN, I prefer using >= and < as needed.
If you want active for the whole month of May, then:
select *
from t
where t.StartDate <= date('1000-05-01') and t.StopDate >= date('1000-05-31')
If you want active for any days in May:
select *
from t
where t.StartDate <= date('1000-05-31') and t.StopDate >= date('1000-05-01')
The StopDate limits may be off by one, depending on whether or not the date of the stop date is considered an active date.
This may also be complicated by whether your data goes by the calendar year, because you have a problem with your data structure. StartDate can be larger than StopDate, which would imply a year wrap. To handle year wraps, try this:
select *
from t
where (t.StartDate < t.StopDate and t.StartDate <= date('1000-05-01') and t.StopDate >= date('1000-05-31')) or
(t.StartDate > t.StopDate and ( t.StartDate <= date('1000-05-01') or t.StopDate >= date('1000-05-31')))
I want to return results from my table that have a date less than today's date. Here is my statement:
$today = date('Y-m-d');
SELECT * FROM events WHERE STR_TO_DATE('$today', '%Y-%m-%d') > wp_eventscalendar_main.eventStartDate
I can select events in the future without a problem but when I try to select events in the past I don't get any results. My column 'eventStartDate' is set as a Date. Is there some kind of special operator I should be using for this?
Thanks,
You probably need:
SELECT * FROM events WHERE CURDATE() > wp_eventscalendar_main.eventStartDate
MySQL tries to compare a String to a Date, which won't work reliably.
You can Use
select from table_name where date < curdate()
I am trying to check if a regulation's date reminder is today and regulation's date end not yet passed then I do not want it to display. The problem is that the query that I made isn't working in mysql. Can anyone help me to revise my query?
Here is my query:
$query="select * from t_regulation where dt_reminder >= '$today' and dt_ended ='$today'"
This is assuming that your dt_reminder columns type is DATETIME, and not some sort of timestap.
SELECT * FROM t_regulation WHERE DATE(dt_reminder) >= CURDATE() AND DATE(dt_ended) = CURDATE()
You can do many funky things with date functions;
Mysql date/time functions
Very frequently I run into the problem that my date variable is a string that is not properly formatted for the default date time stamp in Mysql.
Remember it should be 'yyyy-mm-dd' for this comparison.
Also, for since the 'date ended' has not yet passed, shouldn't it be:
$query="select * from t_regulation where dt_reminder >= '$today' and (dt_ended > '$today' or dt_ended is null)"