A specific way to pull out message list - mysql

I have a project where I am making a virtual phone and currently I need to have a list of people i've messaged. Basically the list you see before going into a private chat with X person.
My problem is currently I can't seem to find the solution to only get latest message I have with X person.
The issue with this is that if I've sent a message to X person and the X person have sent a message to me, i basically get 2 messages in the database when trying to pull out the information, where I only need to get the latest.
This is what I get when doing following and I ALMOST get what I want.
In JQUERY if I remove all where sender = my phone number i would run into a different issue.
https://i.imgur.com/JJzRl6M.png
I've tried following sql
SELECT msg_id, sender, receiver, sender_msg, receiver_read, MAX(msg_date)
FROM nl_phone_messages WHERE sender = '545-3169' OR receiver = '545-3169' GROUP BY sender, receiver
In this case you have to look at the numbers 114-5437 and 545-3169
msg_id 5 should not be in this list as it's not the latest message with this person where msg_id 24 is the latest with that specific person
https://i.imgur.com/WTzRmYv.png
I hope you understand my issue, ask if got any question - Thank you!

Related

Get All Tickets in OSticket

Can someone help me figure out how I can retrieve all tickets? I read online and saw that there's no API to do this yet? I also read that i can write some sql code to retrieve them?
My objective is: Check OSticket to see if the ticket with the same subject is created more than 3 times, then to basically alert me ( for now it can just be a message in Powershell that says it, as I'm scripting in PS).
For that I need to retrieve all tickets in the OSticketDB. Since I just have it locally for now, I have a sql DB setup but I don't see something along the lines of ost_tickets? Not sure how I can retrieve tickets that have been duplicates from same subject.
I'm not sure I understand your question correctly. But here is SQL query, that will return all tickets, where subject has occurred more than 3 times.
SELECT
cdata.ticket_id,
cdata.subject,
ticket.number,
subjectstable.subjectcount
FROM
osticketdb.ost_ticket AS ticket
INNER JOIN osticketdb.ost_ticket__cdata AS cdata ON ticket.ticket_id = cdata.ticket_id
INNER JOIN
(SELECT subject, COUNT(*) as subjectcount FROM osticketdb.ost_ticket__cdata GROUP BY subject) AS subjectstable
ON subjectstable.subject = cdata.subject
WHERE subjectstable.subjectcount > 3

How to create a "Read Message" query from table messages and messages viewed?

I would like to get the messages that someone hasnt read... it could be a count o just a "1" if there are pending messages to read.
The trick is that there are many " users" shareing the same system. So if I usear "A" reads a message from the table then the notification wont appear anymore to A, but for "B" there should be a notification of pending messages. They are sharing the same message lets say.
I create a query that works somehow , but I know is not 100% right.
I did review
Querying conversations from messages table
sql messages table query
In the example below is the deal.
"A" last viewbymessage for the docid 93 was on 2019-01-28 10:02:15, then user B send a new message BUT never reads the message sent by "A", so in my query, "A" will never be able to see there was a new message since he was the last to see if, and I not using the MessageTable only the Messages_View .. I know this is the wrong part, but im just stating how I used to have it.
SELECT B.*
FROM Comments_Viewed_Tbl B LEFT JOIN Comments_Viewed_Tbl C
ON (B.DOCID =C.DOCID and B.Date_Viewed < C.Date_Viewed)
WHERE C.Date_Viewed IS NULL and B.viewedby <>'A' and
B.RPDOC = 93 and B.Country ='USA'
*sorry for the image, I did try to put it as text but the system format irt ugly
How would be the best approach to do the query.
In this scenario A should have an alert or counter of the new message as also B since he/she didnt check it and just send a new one.
So adding a comment is the same as sending a message?
From my point of view, you need to add the CommentID column to the Comments_Viewed_Tbl, otherwise you will never be able to see the read status of each specific comment, only for the whole document.
Otherwise you will need to assume that the last person to add a comment to the document has read all previous comments.

Group Conversation by latest message Laravel

I can't seem to figure this out since mysql is not my strong suit. I've done a bit of research here but can't put two and two together, so I'm turning to the community for some help.
I am building a chat function inside my Laravel app where two users can talk to each other. I can't figure out how to build the inbox (group the conversations together) portion of things. I've figured out how to get last message grouped by sending it.
The following is inside my User Model:
public function lastMessages() {
return $this->hasMany('App\Message', 'recipient_id')
->select('*')
->join(DB::raw('(Select max(id) as id from messages group by author_id) LatestMessage'), function($join) {
$join->on('messages.id', '=', 'LatestMessage.id');
})
->orderBy('created_at', 'desc');
}
My messages table consists of the following: author_id, recipient_id, messaged, viewed.
What I need to do is group messages, whether incoming our outgoing and display the latest message as inbox entry and once I click on that messages, the rest of the conversation pops up on the page.
My current sql shown above only gives me 1 last message, not the whole conversation.
Thanks in advance for all the help.
Firstly, I think you should keep the relationship function separate from logical(query) function.
If I understood your question correctly , you should do something like this :
Message::where('recipient_id',$userid)->orWhere('author_id',$userid)->orderBy('created_at','desc')->get();
This will give you messages send or received by user. Please clarify your question with Author and Message models if this is not what you want.

Not like on Array field

here is the problem I'm stuck with:
I'm using Rails 4 & MySQL
I've Message which have one sender and one recipient.
I want to be able to archive messages but if sender archive a message, the recipient still can access to the message until he archive it too.
I've serialize a field :
serialize :archived_by, Array
which contains which user archived the message
but I can't figure out how to query with it.
Message.where("archived_by like ?", [1].to_yaml)
works well, returning messages archived by User '1'
Message.where.not("archived_by like ?", [1].to_yaml)
won't work, returning nothing
I would like to find something else than using a classic many to many ...
Thanks!
UPDATE
I finally decided to add 2 fields, one for the sender & one for the recipient to know which archived the message. If someone has the proper way to do this, tell us :)
If you are using postgresql you could query the informations.
As in answer Searching serialized data, using active record described, the downsize of serializer at least under mysql is, that you byepass native db abstraction.

Inbox Functionality with MYSQL

So I am writing a simple inbox private message system.
My table schema is as follows:
messageID
message
sender id
receiver id
date sent
read ( 0 = no , 1 = yes)
I am able to show the messages for the user by relating his userID to receiverID. However I also want to show the messages he has sent in the inbox to the user.
For example his inbox should show:
Darth Vader - 3 New messages
Luke - 0
new messages (0 because either I read
all of them OR i sent him the message
and he has not replied).
But what i can only come up with is
Darth Vader - 3 New messages.
Can I get any help with how I can accomplish this SQL call?
EDIT: To Clear the Confusion
I am neither Luke or Darth. I have received 3 New messages from darth, and I have sent a message to luke.
EDIT**
Basically I want to be able to make the inbox like how an SMS app would be, where you can see the sms you just sent in a list of sms's by your friends.
SELECT users.username, count(messageID) AS unread
FROM users, messages
WHERE messages.senderID=<USER ID>
AND messages.receiverID=users.userID
AND messages.read=0
Just to make sure I got this right, this query will show all the users I have sent a message to and count the number of those messages which have not been read.