简体   繁体   中英

Inner Join Same table for values

My Table has entries as below. Where object id would be same. I need to pick up the other value of object id which has "HR" / "Infra" for that particular object_id.

--------------------------------------------------
|id     | field_id  | object_id     | value_text
----------------------------------------------------    
|182    |   11      | 149           | HR
|183    |   13      | 149           | Learning Management   
|184    |   11      | 150           | HR 
|185    |   13      | 150           | Compensation & Benefits   
|186    |   11      | 151           | HR 
|187    |   13      | 151           | Leave Management  
|188    |   11      | 152           | HR 
|189    |   13      | 152           | Compensation & Benefits   
|190    |   11      | 153           | Infra 
|191    |   13      | 153           | Hardisk   
|192    |   11      | 154           | Infra 
|193    |   13      | 154           | Software  
|194    |   11      | 155           | Infra
|195    |   13      | 155           | Network   
|196    |   11      | 156           | HR 
|197    |   13      | 156           | Position Management   
|198    |   11      | 139           | HR 
|199    |   13      | 139           | Compensation & Benefits   
|200    |   11      | 157           | Infra 
|201    |   13      | 157           | Network       

Query:

SELECT d.value_text 
FROM dynamic d 
INNER JOIN dynamic dv ON d.object_id = d.object_id AND d.field_id = "13" 
                     AND dv.field_id = "11" AND dv.value_text = "HR"

output required:

--------------
| value_text |
--------------
| Learning Management 
| Compensation & Benefits
| Position Management

Second Query:

SELECT d.value_text 
FROM dynamic d 
INNER JOIN dynamic dv ON d.object_id = d.object_id AND d.field_id = "13" 
                     AND dv.field_id = "11" AND dv.value_text = "Infra"

output required:

--------------
| value_text |
--------------
| Network 
| Software
| Hardisk

How to modify the query to get the details. .

you want to have the both queries combined?

then you can do this:

SELECT d.value_text FROM dynamic d 
INNER JOIN dynamic dv ON d.object_id = d.object_id AND
d.field_id = "13" AND dv.field_id = "11" 
AND (dv._obejct_id = "Infra" OR dv._obejct_id = "HR")

I hope this is what you needed.

not sure what you are asking for, but try these:

select
      dv.id, dv.field_id, dv.object_id, dv.value_text
from dynamic d
inner join dynamic dv on dv.field_id = 13 and d.object_id = dv.object_id
where d.field_id = 11
and d.value_text = 'HR'
;

select
      dv.id, dv.field_id, dv.object_id, dv.value_text
from dynamic d
inner join dynamic dv on dv.field_id = 13 and d.object_id = dv.object_id
where d.field_id = 11
and d.value_text = 'Infra'
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM