I'm building a chat app retrieving chat history when a certain chat is expanded but i want a function or functions that can help me get chats that are 5 minutes within each other and are sent by the same user let's say given this table below
id
message
msg_owner_id
msg_reciever
msg_time
msg_type
seen
seen_time
note(not part of table)
1
hey
20
6
10:42:51
interchats
0
<----- 20 sent to 6
2
hi
20
6
10:43:20
interchats
0
<------20 sent to 20
3
am fine
20
6
10:44:11
interchats
0
<------20 sends to 6 again
4
mannnn
6
20
10:45:02
interchats
0
<-- 6 replies to 20
5
mannnn
20
11
10:53:02
interchats
0
<-- 20 sends to 11
I have used the following code however some values are missing
SELECT GROUP_CONCAT(id), DATE_FORMAT(msg_time, '%Y-%m-%d %H:%i'),
id as ID, msg_owner_id as MSG_OWNER, msg_reciever as MSG_RECEIVER,
message as MESSAGE
FROM chats WHERE msg_time >= :msg_time - INTERVAL 5 MINUTE AND id >= :id
GROUP BY 2 ORDER BY 2
let's say we need to start from id 1 and timestamp 10:42:51, how do I select all the chats sent within a 5 minutes interval from ID 1 and timestamp 10:42:52
Desired result should be:
the following IDs returned 1, 2 and 3
Related
Hi all i have a table like this
ID
result
grade
date
score
A
pass
1
02/27
10
A
fail
null
02/25
null
A
pass
1
02/24
10
A
pass
1
02/23
10
A
pass
1
02/22
10
A
pass
1
02/21
10
A
pass
1
02/18
10
A
pass
1
02/16
10
A
pass
1
02/15
10
A
pass
1
02/03
10
A
pass
1
02/02
10
What I want is to remove the entry if result, grade and score are the same and the date is within 15 days of the latest entry. So the final table would look like this:
ID
result
grade
date
score
A
pass
1
02/27
10
A
fail
null
02/25
null
A
pass
1
02/03
10
Any methods of doing it in MySQL?
i have table(readings) columns are
id| date| meter_id| previous| current| units_cons
1 21/04/2019 20 23 25 2
2 23/05/2019 20 25 30 5
3 22/06/2019 20 30 36 6
4 22/04/2019 3 8 10 2
5 27/5/2019 3 10 15 5
my challenge is i cannot pull the previous month values,at the current date on my form. when i search the column meter_id
$meter_id =$_REQUEST['id'];
$result = mysqli_query($conn,"SELECT prev,pres,unit_cons,date
FROM readings where id='$meter_id'");
when i run the code i get values for a different meter user.e.g
when i click on meter_id 3 i get values for meter_id 20
ensure you are using the right key either id or meter_id because from your request it reads $_REQUEST['id']; be consistent
$id =$_REQUEST['id'];
$result = mysqli_query($conn, "SELECT previous, current, (current - previous) AS reading_diff, unit_cons, date FROM readings where id={$id}");
Note where reading_diff is the computer difference between the current and previous meter readings.
I have a table that I use to record the activity log of an application. Now I have to get all records from entity X that have been published in some range of dates. So if a record has been published and later unpublished, it doesn't have to appear in the results.
I don't know how to explain it really, it's find the last appearance of each one and then catch values that are "1" or "0", on depends that I need in each case.
A simplified example (the real table has more fields and more data):
id user_id date model main_relation_id field new_value
1 24 2017-03-21 A 1 publish 1
2 24 2017-03-21 A 2 publish 1
3 24 2017-03-22 A 3 publish 0
4 24 2017-03-22 A 2 update some text
5 24 2017-03-23 A 1 publish 0
6 24 2017-03-23 A 1 update some text
7 24 2017-03-24 A 3 publish 1
8 24 2017-03-24 A 2 publish 0
9 24 2017-03-24 A 2 update some text
10 24 2017-03-25 A 1 publish 1
11 24 2017-03-25 A 2 publish 1
11 24 2017-03-26 A 3 publish 0
I need to get main_relation_id, filtering by model and date, so if I want to get all registers from model A that have been published between 2017-03-21 and 2017-03-24 I'll get:
model_main_relation_id
1
3
and if I want to get all registers that have been unpublished in the same dates, the result have to be:
model_main_relation_id
2
How can I get this result?
So, you would like to filter on the latest status by main_relation_id, whether the particular main_relation_id has been published (field='publish'; new_value=1) or unpublished (field='publish'; new_value=0) within a period.
Since the dates within the date field of the sample data are equal for multiple records, therefore I must assume that a higher value in the id field means later event. Therefore the max(id) per main_relation_id would yield the latest even record.
What I would do is to get the max(id) per main_relation_id within a date range for a particular model in a derived table where field is 'publish' and join this back to your table to find out whether the particular main_relation_id was published:
select table.main_relation_id
from table
inner join
(select main_relation_id, max(id) as maxid
from table
where date>=... and date<=... and `field`='publish' and model='...'
group by main_relation_id) t on table.id=t.maxid
where table.new_value=1
You need to substitute the filter criteria in place of the .... If you would like to get the unpublished data, then replace table.new_value=1 criterion with table.new_value=0.
I am trying to create a Trending Posts database call for my homepage.
The best way I thought of doing this, would be by making 2 integer tables.
The problem I am having, is sorting these results.
Here is what my table looks like, I keep track of the last 10 trending posts.
name trending clicks
post1 10 5
post2 9 15
post3 8 12
post4 7 10
post5 6 8
post6 5 8
post7 4 22
post8 3 18
post9 2 8
post10 1 8
The Trending Position is updated every 4 hours and clicks are reset to 0.
From this data, I need to pull the posts in this order
name trending clicks
post6 5 22
post8 3 18
post2 9 15
post3 8 12
post4 7 10
post5 6 8
post9 2 8
post10 1 8
post1 10 5
Here is another example:
name trending clicks
post1 10 0
post5 6 9
post2 9 0
post3 8 0
post4 7 0
post6 5 0
post7 4 0
post10 1 3
post8 3 0
post9 2 0
I need to sort these results, based on the clicks ONLY if they are above the trending position.
Example: If post #8 get's more clicks than position #4, it will take it's spot and keep the order of the trending, but use the clicks to also order the data.
The original posts will keep their position until a trending post has gotten more clicks than the position to take it's place.
The problem i'm trying to resolve, is when I reset the trending data. I reset the data every 4 hours but want to keep the positions and weight on each trending post.
Did I explain this well enough and can anybody please help me?
Why this doesnt work for you ?
SELECT name, trending, clicks
FROM YourTable
ORDER BY clicks DESC
But if you want keep previous trend, then you need add a weight column.
SELECT name, trending, clicks, weight
FROM YourTable
ORDER BY ((clicks + weight) /2) DESC
Instead of reset your trend every 4h wouldnt be easy calculate the clicks on the last 4h. You have to record datatime for each click
SELECT name, count(clicks) as clicks
FROM YourTable
WHERE datetime > DATEADD(h,-4,GETDATE())
GROUP BY name
ORDER BY count(clicks) DESC
Barcode Incoming Outgoing Stock Date
1 10 5 5 2015-1-3
2 3 2 1 2015-1-3
1 2 1 1 2015-1-4
My current Query's output is shown above. What i need is to add the stock from the previous date of the same barcode to the latest. As Shown below
Barcode Incoming Outgoing Stock Date
1 10 5 5 2015-1-3
2 3 2 1 2015-1-3
1 2 1 6 2015-1-4
Is there a way to make this possible? my current query is nested but looks like this. not working though
Select
Barcode,
incoming,
outgoing,
(incoming - outgoing + Stock_prev_value) as Stock,
currentDate
from (select ....
)as myquery
Help me please.
currentDate => DATE_SUB(NOW(),INTERVAL 1 DAY)
instead of requesting you can also trigger/add a table 'stock_history' per day :
-barcode
-stock
-date