I have users and orders tables with this structure (simplified for question):
USERS
userid
registered(date)
ORDERS
id
date (order placed date)
user_id
I need to get array of users (array of userid) who placed their 25th order during specified period (for example in May 2019), date of 25th order for each user, number of days to place 25th order (difference between registration date for user and date of 25th order placed).
For example if user registered in April 2018, then placed 20 orders in 2018, and then placed 21-30th orders in Jan-May 2019 - this user should be in this array, if he placed 25th (overall for his account) order in May 2019.
How I can do this with MySQL request?
Sample data and structure: http://www.sqlfiddle.com/#!9/998358 (for testing you can get 3rd order as ex., not 25th, to not add a lot of sample data records).
One request is not required - if this can't be done in one request, few is possible and allowed.
You can use a correlated subquery to get the count of orders placed before the current one by a user. If that's 24 the current order is the 25th. Then check if the date is in the desired range.
SELECT o1.user_id,
o1.date,
datediff(o1.date, u1.registered)
FROM orders o1
INNER JOIN users u1
ON u1.userid = o1.user_id
WHERE (SELECT count(*)
FROM orders o2
WHERE o2.user_id = o1.user_id
AND o2.date < o1.date
OR o2.date = o1.date
AND o2.id < o1.id) = 24
AND o1.date >= '2019-01-01'
AND o1.date < '2019-06-01';
The basic inefficient way of doing this would be to get the user_id for every row in ORDERS where the date is in your target range AND the count of rows in ORDERS with the same user_id and a lower date is exactly 24.
This can get very ugly, very quickly, though.
If you're calling this from code you control, can't you do it from the code?
If not, there should be a way to assign to each row an index describing its rank among orders for its specific user_id, and select from this all user_id from rows with an index of 25 and a correct date. This will give you a select from select from select, but it should be much faster. The difficulty here is to control the order of the rows, so here are the selects I envision:
Select all rows, order by user_id asc, date asc, union-ed to nothing from a table made of two vars you'll initialize at 0.
from this, select all while updating a var to know if a row's user_id is the same as the last, and adding a field that will report so (so for each user_id the first line in order will have a specific value like 0 while the other rows for the same user_id will have a 1)
from this, select all plus a field that equals itself plus one in case the first added field is 1, else 0
from this, select the user_id from the rows where the second added field is 25 and the date is in range.
The union thingy is only necessary if you need to do it all in one request (you have to initialize them in a lower select than the one they're used in).
Edit: Well if you need the date too you can just select it along with the user_id, but calculating the number of days in sql will be a pain. Just join the result table to the users table and get both the date of 25th order and their date of registration, you'll surely be able to do the difference in code.
I'll try building an actual request, however if you want to truly understand what you need to make this you gotta read up on mysql variables, unions, and conditional statements.
"Looks too complicated. I am sure that this can be done with current DB structure and 1-2 requests." Well, yeah. Use the COUNT request, it will be easy, and slow as hell.
For the complex answer, see http://www.sqlfiddle.com/#!9/998358/21
Since you can use multiple requests, you can just initialize the vars first.
It isn't actually THAT complicated, you just have to understand how to concretely express what you mean by "an user's 25th command" to a SQL engine.
See http://www.sqlfiddle.com/#!9/998358/24 for the difference in days, turns out there's a method for that.
Edit 5: seems you're going with the COUNT method. I'll pray your DB is small.
Edit 6: For posterity:
The count method will take years on very large databases. Since OP didn't come back, I'm assuming his is small enough to overlook query speed. If that's not your case and let's say it's 10 years from now and the sqlfiddle links are dead; here's the two-queries solution:
SET #PREV_USR:=0;
SELECT user_id, date_ FROM (
SELECT user_id, date_, SAME_USR AS IGNORE_SMUSR,
#RANK_USR:=(CASE SAME_USR WHEN 0 THEN 1 ELSE #RANK_USR+1 END) AS RANK FROM (
SELECT orders.*, CASE WHEN #PREV_USR = user_id THEN 1 ELSE 0 END AS SAME_USR,
#PREV_USR:=user_id AS IGNORE_USR FROM
orders
ORDER BY user_id ASC, date_ ASC, id ASC
) AS DERIVED_1
) AS DERIVED_2
WHERE RANK = 25 AND YEAR(date_) = 2019 AND MONTH(date_) = 4 ;
Just change RANK = ? and the conditions to fit your needs. If you want to fully understand it, start by the innermost SELECT then work your way high; this version fuses the points 1 & 2 of my explanation.
Now sometimes you will have to use an API or something and it wont let you keep variable values in memory unless you commit it or some other restriction, and you'll need to do it in one query. To do that, you put the initialization one step lower and make it so it does not affect the higher statements. IMO the best way to do this is in a UNION with a fake table where the only row is excluded. You'll avoid the hassle of a JOIN and it's just better overall.
SELECT user_id, date_ FROM (
SELECT user_id, date_, SAME_USR AS IGNORE_SMUSR,
#RANK_USR:=(CASE SAME_USR WHEN 0 THEN 1 ELSE #RANK_USR+1 END) AS RANK FROM (
SELECT DERIVED_4.*, CASE WHEN #PREV_USR = user_id THEN 1 ELSE 0 END AS SAME_USR,
#PREV_USR:=user_id AS IGNORE_USR FROM
(SELECT * FROM orders
UNION
SELECT * FROM (
SELECT (#PREV_USR:=0) AS INIT_PREV_USR, 0 AS COL_2, 0 AS COL_3
) AS DERIVED_3
WHERE INIT_PREV_USR <> 0
) AS DERIVED_4
ORDER BY user_id ASC, date_ ASC, id ASC
) AS DERIVED_1
) AS DERIVED_2
WHERE RANK = 25 AND YEAR(date_) = 2019 AND MONTH(date_) = 4 ;
With that method, the thing to watch for is the amount and the type of columns in your basic table. Here orders' first field is an int, so I put INIT_PREV_USR in first then there are two more fields so I just add two zeroes with names and call it a day. Most types work, since the union doesn't actually do anything, but I wouldn't try this when your first field is a blob (worst comes to worst you can use a JOIN).
You'll note this is derived from a method of pagination in mysql. If you want to apply this to other engines, just check out their best pagination calls and you should be able to work thinks out.
I have epoch timestamps into "PART_EPOCH" column, table name is "crud_mysqli"
I would like to select associated "PART_ID" value for the next FUTURE timestamps. (avoid a research into past timestamps)
The following MySQLI query should select the MIN (next) value within the future : > now.
But it does not return anything.
It does return expected return if i state clauses seperately,
combining clauses as below returns no result.
Would you please tell me what is wrong here :
// Find next event PART_ID name :
// SELECT lowest (next) PART_ID value in the future (do not select winthin past PART_EPOCH values)
$query = "SELECT
PART_ID
FROM crud_mysqli
WHERE (PART_EPOCH = (SELECT MIN(PART_EPOCH) FROM crud_mysqli))
AND (PART_EPOCH > UNIX_TIMESTAMP(NOW()))
";
WHERE (PART_EPOCH = (SELECT MIN(PART_EPOCH) FROM crud_mysqli))
Here you say to only take the entry with the lowest timestamp, which is probably somthing in the past.
AND (PART_EPOCH > UNIX_TIMESTAMP(NOW()))
And here you say, that it should be in the future. The two conditions are excluding each other, if you have any entry with the timestamp in the future.
So you need to put the second condition into the subquery:
SELECT
PART_ID
FROM crud_mysqli
WHERE PART_EPOCH = (
SELECT MIN(PART_EPOCH)
FROM crud_mysqli
WHERE PART_EPOCH > UNIX_TIMESTAMP(NOW())
)
That means: "take the entry with the lowest timestamp in the past"
However.. you can as good do the following:
SELECT PART_ID
FROM crud_mysqli
WHERE PART_EPOCH > UNIX_TIMESTAMP(NOW())
ORDER BY PART_EPOCH ASC
LIMIT 1
The result would only differ if you have two entries with the same timestamp. In that case the first query would return both of them - the second query only one.
I'm pretty bad with dates.
I have a mysql table with one field, which is OF DateTime type, called HoraRegistratBBDD.
What I want to do is to select data (any kind of data) from a specific day. So far I was doing this:
SELECT COUNT(*)
FROM
(
SELECT mydata
FROM mytable
WHERE DATE(`HoraRegistratBBDD`) = '".$fecha."' AND
FetOPerdutIMotiu = '1'
GROUP BY Partit,
mydata
) AS Col;
Where $fecha is something like "2016-09-03". THIS WORKS.
But I have a problem. When my HoraRegistratBBDD has (for example) this value:
2016-09-02 10:28:41
I would like to substract 15 hours from it. Meaning that I would like to treat this value like it's actually
2016-09-01 19:28:41
How can I do my query considering that I want to substract hours from it (therefore, day will change sometimes)?
If you want to subtract 15 hours from the HoraRegistratBBDD column, then you can use DATE_SUB:
SELECT mydata FROM mytable
WHERE DATE_SUB(HoraRegistratBBDD, INTERVAL 15 HOUR) = ...
The function that you are looking for is DATE_SUB.
Here are a few links:
http://www.w3schools.com/sql/func_date_sub.asp
How to subtract 3 hours from a datetime in MySQL?
The first one shows you how it works and the other one is a similar question and it has been answered.
SELECT COUNT(*)
FROM
(
SELECT mydata, DATE_FORMAT(HoraRegistratBBDD,'%Y-%m-%d') AS niceDate
FROM mytable
WHERE
FetOPerdutIMotiu = '1'
HAVING niceDate = '".$fecha."'
GROUP BY Partit,
mydata
) AS Col;
This query below selects all rows that have a row with the same father registering 335 days or less since earlier registration. Is there a way to edit this query so that it does not eliminate the duplicate row in the output? I need to see all instances of the registration for that father within 335 days of each other.
SELECT * FROM ymca_reg a later
WHERE NOT EXISTS (
SELECT 1 FROM ymca_reg a earlier
WHERE
earlier.Father_First_Name = later.Father_First_Name
AND earlier.Father_Last_Name = later.Father_Last_Name
AND (later.Date - earlier.Date < 335) AND (later.Date > earlier.Date)
My current query is:
SELECT ymca_reg.* FROM ymca_reg WHERE (((ymca_reg.Year) In (SELECT Year FROM ymca_reg As Tmp
GROUP BY Year, Father_Last_Name, Father_First_Name
HAVING Count(*)>1
And Father_Last_Name = ymca_reg.Father_Last_Name
And Father_First_Name = ymca_reg.Father_First_Name)))
ORDER BY ymca_reg.Year, ymca_reg.Father_Last_Name, ymca_reg.Father_First_Name
This query does return all the duplicates for review correctly, but it's terribly slow because it doesn't use a join and as soon as I add the date criteria it only returns the later row. Thanks.
I think you want something like this:
SELECT *
FROM ymca_reg later
WHERE EXISTS (SELECT 1
FROM ymca_reg earlier
WHERE earlier.Father_First_Name = later.Father_First_Name AND
earlier.Father_Last_Name = later.Father_Last_Name AND
abs(later.Date - earlier.Date) < 335 and
later.Date <> earlier.Date
);
This should return all records that have such duplicates. Note that "later" and "earlier" are no longer really apt descriptions, but I left the names so you can see the similarity to your query.
I have a search result query for posts. And I want posts written by myself(userId = 27) to be first in query result and rest ordered by time stamp. Can anyone give me query for that in mysql?
select *
from posts
order by
if (userid=27, -1, any_timestamp_include_zero);
include your full table schema help much better
How about something like:
select * from post
order by
case
when userid = 25 then '0001-01-01 00:00:00'
else my_timestamp
end
(formatting the '0001-01-01' part appropriately for MySql)
Something simple like this:
SELECT * FROM POST WHERE userId = 25
UNION
SELECT * FROM POST WHERE userId <> 25 ORDER BY TIMESTAMP_FIELD
Could work for your need?