Solved

mysql query messages and replies to the same topic

Posted on 2009-07-07
2
207 Views
Last Modified: 2012-05-07
I have table described below of messages. I want to display all messages initiated by a specific user, as well as all other messages that replied to the same topic as that message. Then I would need that sorted by date. So essentially, it creates a list of messages, and responses to those messages (so users can see what they wrote, and what people responded after them).

What it is essentially excluding is, messages written by other users on topic the subject user is not participating in, or messages written by other users on a topic before the subject user has participated.

I've attempted what I think the query might look like
messages table

------------------

message_id

topic_id

user_id (person who created the message)

date
 

attempted query

-------------------

select * from messages 

where user_id = '14'

or where exists (select * from messages as messages2 where messages.topic_id = messages2.topic_id and user_id != '14' and messages2.date > messages.date)

Open in new window

0
Comment
Question by:MeridianManagement
2 Comments
 
LVL 9

Expert Comment

by:waygood
ID: 24802051
How are you distinquishing which message is a start of a topic? as you've only provided info on the messages.
Do you have a topic table too? as placing the person who started the topic in this table also would be easier
0
 
LVL 2

Accepted Solution

by:
MeridianManagement earned 0 total points
ID: 24804275


this code does what I am looking for.
select * from messages 

where user_id = '14'

or exists (select * from messages as messages2 where messages.topic_id = messages2.topic_id and user_id = '14' and messages2.date > messages.date)

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now