Solved

mysql query messages and replies to the same topic

Posted on 2009-07-07
2
208 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

863 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

24 Experts available now in Live!

Get 1:1 Help Now