skip to Main Content

Hey guys I have a long table in my Database and i want select all records that have the same id and parent_id.

id name parent_id
2 lorem 2

Second case:

In the second case there are ids and parent_ids in different rows.

enter image description here

Thanks in advance.

3

Answers


  1. SELECT ID,
           PARENT_ID 
    FROM TABLENAME 
    WHERE ID=PARENT_ID
    
    Login or Signup to reply.
  2. I think you can use this structure:

    SELECT [column list] FROM [tablename] WHERE id=parent_id
    
    Login or Signup to reply.
  3. you simply need to put an = sign for the column that you want to have the same values. Passing this condition to the where clause will filter the rows to show only the ones where the two columns are equal.

    select * from <your_table_name> where id = parent_id
    
    • <your_table_name> = pass your table name

    for your case 2 where you want the rows that matches the id = parent_id but they are not in the same row:

    • you need a self-join
    SELECT 
    *
    FROM
        <table_name> as t1
    INNER JOIN <table_name> as t2
    on t1.id = t2.parent_id
    

    if in different tables:

    SELECT * FROM table1 WHERE parent_id in (SELECT id FROM table2) 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search