Solved

mysql query messages and replies to the same topic

Posted on 2009-07-07
2
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 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