tags:

views:

34

answers:

2

I currently have 2 tables in a MySql database.

Structure:

messages
id | content | user_id | time

submessages
id | subcontent | msg_id | user_id | time

In the submessages table, column msg_id is a foreign key, referencing the id column in table messages.

Now i need to query from php

$cur_userID = $user->current_id; // current user id.
SELECT * FROM messages WHERE id > '{$_GET['id']}' // problem here

How do I query the submessages table for subcontent that was posted by other users - exlcuding current user?

Meaning anything with a user_id equal to $cur_userID shouldn't be displayed.

Thank you.

A: 
"SELECT * FROM messages WHERE id != " . intval($_GET['id']);
fire
Actually, what this appears to do is give all the messages EXCEPT the one you are interested in. I think "id" is just the id of the message, not the user.
MJB
isn't not equals `<>` in SQL, not `!=`?
LeguRi
@Richard JP Le Guen: In standard SQL, yes. However MySQL accepts both forms: http://dev.mysql.com/doc/refman/5.0/en/non-typed-operators.html
ircmaxell
But none of the examples use `!=` http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_not-equal and I've only been using MySQL for some time and I feel like I've only ever gotten error messages when I tried to use `!=`... I still feel skeptical.
LeguRi
A: 

You want to get the submessage detail, right? This query would get all the info from the message table, plus all the info from EACH of the sub-messages. Thus the join. But it would exclude sub-messages (answers?) that have the same user_id as the messages (questions?).

SELECT m.* sm.*
FROM messages m
INNER JOIN submessages sm
  ON m.id = sm.msg_id
WHERE id > '{$_GET['id']}'   // dont know why gt is used here...
  AND m.user_id <> sm.user_id

And I ignored all the formatting necessary to put this into PHP, since really the question as I understand it is how should the SQL be written.

As an aside, I don't get the id > $_GET['id'] section. It appears that you are asking for all messages created AFTER this message, and you want to find the submessages for thos messages. I really can't figure out when this is reasonable, but if that is what you want this is a way that should work. I suspect you want equals, not greater than.

MJB