Is there a difference between regular Mysql and MariaDB with Curdate which is messing up my query? - mysqli

In MYSQL this command used to find all dates before now. < CURDATE() but it stopped working.
For several years I used a select statement to find not completed hotel bookings in the past so I could delete them. Then about 6 months ago my php script started deleting important bookings in the future. There must be some subtle difference in the way that MYSQL is processing my query.
In my database a booking has an arrival day, the length of say is duration.
(so the leaving date is arrival date plus duration)
It is supposed to find bookings where the leaving date was more than 3 days ago. But it is finding bookings in the future too.
DELETE FROM booking WHERE status_id = 1 AND date_add(booking.arrivalday, INTERVAL duration DAY)+3 < CURDATE()
This code worked fine until a few months ago. Did MYSQL make some change that made it stop working.
I have just logged into the database to see which version of mysql I am using.
I was surprised to learn that it is
MySQL version: 10.1.37-MariaDB-cll-lve through PHP extension MySQLi
I am using A1 hosting.
I thought I was using regular MYSQL not maria MYSQL.
I am now starting to suspect that the problem with my query is caused by a difference between regular MYSQL and maria MYSQL.
Can anyone shed any light on this ????

I discovered the solution which is the following.
DELETE FROM booking WHERE status_id = 1 AND date_add(booking.arrivalday, INTERVAL duration DAY) < (CURDATE() -3)
A tiny change of syntax makes the query work.
What caused it to stop working remains a mystery.
The only thing that I can think of is changing from regular PHP to Maria PHP.

Related

PostgreSQL: UNIX Time Dynamic query

I'm making a Power BI report where data that I need to show is stored in a PostgreSQL database.
In the table where I query there is data from 4 years ago until today, but for my report I only need the last week of records (I know that I can filter using Power BI but my goal is make the lighter query as possible).
The fields of the database related to time, are in UNIX Timestamp, so I'm filtering it now by this way:
SELECT
DATABASE.INCIDENT_NUMBER
,DATABASE.SUBMITTER
,DATABASE.CREATE_DATE
,DATABASE.MODIFIED_DATE
,DATABASE.CLOSED_DATE
,DATABASE.SUBJECT
FROM
DATABASE
WHERE
1643670000 < DATABASE.CREATE_DATE
ORDER BY DATABASE.INCIDENT_NUMBER, DATABASE.CREATE_DATE ASC
That is fine, but I want to improve it making a dynamic query which returns the records from last week till today, without putting a constant of UNIX timestamp.
How can I make that?
That's an excellent example why it is a bad idea to store timestamps as numbers. The correct data type is timestamp or timestamp with time zone.
If you had used the correct data type, your condition would be as simple as
WHERE current_date - 7 < database.create_date
But with numbers, you have to convert back and forth:
WHERE to_timestamp(EXTRACT('epoch' FROM current_date) - 7) < database.create_date

LibreOffice date shift in PostgreSQL

I'm managing small PostgreSQL database. Recently I created new date field in one of tables. I'm working on pgAdminIII and everything is fine, but one of the users just reported that while working on LibreOffice all dates are shifted by 10 days to the past.
I just installed LibreOffice to check it and they are shifted on my copy too but by 9 days.
This is simple date field without any default value or constraints. All dates are before year 1900 (1400-1500).
In pgAdmin and console psql client everything is fine.
What can cause such an issue? How to fix that?
EDIT:
Problem occurs in table view only. I can access correct date via Tools > SQL... menu while viewing table data or creating query produces invalid results. It may be format conversion error (from Y-M-D to D.M.Y).

How to get the data of last five days from amazon simple db in iphone application

I want to get the data from table last 5 days records I have seen examples they work fine for other data base but i am using amazon simple db so and sql razor I am using same query but it does not work and shows error
SELECT *
FROM UserContentUsage
WHERE ViewDateTime BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 DAY) AND CURDATE()
Amazon SimpleDB stores everything as UTF-8 strings. All comparisons are done lexicographically. The dates should be in ISO 8601 format so that they can be properly compared in lexicographical order.
See page for more detail.

MongoId perform day / month specific queries on the date

I am using MongoId 3,Rails 3.2 ,Ruby 1.9.3.
I wanted to query only part of a date, such as the day, week or month.
So for example, let's say we need to find all users that signed up on a Wednesday OR
get all users whose birthday is on 25 day Or get all users who born in April month(not specific to particular year here) etc..
In Mysql we have select * from users where extract(dow from created_at) = 3;
But how can we perform this using MongoId.
Here is a pretty handy cookbook on querying data ranges on Mongodb: http://cookbook.mongodb.org/patterns/date_range/ . and you can just create the query almost in the same way on Mongoid, using .where
I think the aggregation framework is a better fit for this. This question discusses the use of date operators in aggregation.
I've not had much experience with Mongoid, but it seems like it should support aggregation just fine, as long as you are using a recent version.

Calculating days between last login and current date

Upon logging into their accounts, each user has their login date and time stored to the database. What I was looking to do however is figure out the amount of days (or preferably convert into months if greater than a month) so that if a user views their profile they can see how active the band are. Also, this could benefit me in terms of keeping active profiles top of the agenda for content on the site so that it doesn't become stale from inactive users content filling up main page content.
I'm using ColdFusion so i'd be looking for a way to find for example how many days ago #lastLogin# was from #now()#. So say if the date of the last login was 23/04/2013 and todays date is 29/04/2013 it would read "Last Active, 1 day ago." However if the last login was 23/03/2013, it would read "Last Active, 1 month ago".
Anybody know how to do this? Thanks.
P.S I currently have no code from testing this as I have no idea where to start in terms of achieving this.
Use DateDiff
<cfset days = dateDiff("d", LoginDateVariable, now()) />
It's as simple as that.
P.S I currently have no code from testing this as I have no idea where
to start in terms of achieving this.
This doesn't answer your direct question but to help you know where to get started, I would strongly suggest reviewing the built in ColdFusion functions and tags that are available to you.
Tags
Tags by function
Functions
Functions by category
Also, Google searches usually land you at the docs, just add "coldfusion" to your search string. Searching google for coldfusion date functions yields very helpful answers, the first of which are a list of all ColdFusion date functions.
Dale's answer is spot on. But I would also suggest returning it as a variable with your query. Let the SQL server do the work. It's very efficient for those types of calculations. Not that CF can't do them well, too. But it's probably more appropriate for SQL to do that lifting. Especially if you're already returning the lastLogin date.
It would be similar to the CF solution:
SELECT ...., lastLogin, DATEDIFF(d, lastLogin, GETDATE()) AS LastLoginDays
FROM ....
WHERE ....
That would give you the number of days. You'd have to decide how you wanted to define a month if you wanted to break it out by month/day. That would get a bit more complex. You could write a SQL function that could be run on both dates and give you an accurate count of days/months/years since last login.
One other thing to keep in mind: Where are the dates being generated? When you insert loginDate into the database, are you doing a now() in CF before you insert it or are you doing a getDate() in SQL when you insert it? Again, I would let the database do your date logic, but you'd want to compare the two dates from the same source. For instance, if your loginDate was a database getDate() then you may not want to compare that to a CF now(). One goes by the datetime of the SQL server and the other goes by the datetime of the CF server. They could be different.