mysql - SQL where clause with multiple keys in same table -


I have a table with two keys and I can not break the table in two.

The first key, the ID is a counter, the second key is parent_id. For example:

id, parent_id, text
1, 0, Hello Steve - this bill is 2, 1, hi - how are you bills?
3, 0, Good morning Janice 4, 3, Hello - Good night to you

The first record is the guardian record of the conversation and the second is the child's record.

What I'm having difficulty is writing a query that gives both records for the same conversation when you pass any ID for example:

 Select from  Select * From the table where id = 2 or parent_id = (select parent_id from the table where id = 2) * Select * from the table where id = 1 or Parent_id = (select parent_id where the id = 1)  

The first query will work, return the records with ID 1 and 2. The second will not be because it will return the line with the id as well 3 because if you pass 1, you will get a zero for the parent.

I am sure that it is something that is missing me due to paralysis by analysis.

Thanks

This query will be retrieved:

  • Specify the specified child record,
  • specify the parent record and all its single-level children
  select DISTINCT * FROM Select from the guardian SELECT * to the table WHERE parent_id = @SomeID Union All - ID * From the table WHERE ID = @S omeid - and parent self or id = (SELECT parent_id table WHERE id = @SomeID) union all All with parents Select the siblings SELECT * from the table WHERE parent_id = (SELECT parent_id table WHERE id = @ SOID) and parent> 0) FK order ID  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -