mysql inner join if value = true ( share system ) users


|  id  | name  |
|------|-------|
|   1  | John  |
|   2  | Clark |


groups


|  id  | group_name|
|------|-----------|
|   1  | groupName |


posts
authorType*
1- user
2- group


|  id  | post_data   | author | authorType* |
|------|-------------|--------|-------------|
|   1  | Lorem Ipsum |    1   |      1      |
|   2  | Lorem Ipsum |    1   |      1      |
|   3  | Lorem Ipsum |    2   |      1      |
|   4  | Lorem Ipsum |    1   |      2      |
..........


user_posts
share_in*
1- user
2- group


|  id  | user_id | sender_id | post_id | shared_id | share_in* |
|------|---------|-----------|---------|-----------|-----------|
|   1  |    1    |     1     |    1    |    null   |   null    |
|   2  |    1    |     1     |    2    |    null   |   null    |
|   3  |    2    |     2     |    3    |    null   |   null    |
|   4  |    1    |     1     |    3    |      3    |     1     |
|   5  |    1    |     1     |    3    |      1    |     2     |


How to execute the query

select * from user_posts where user_id = 1
if( user_posts.share_in = 1 ) { join users on user_posts.shared_id = users.id }
else join groups on user_posts.shared_id = groups.id
While I don't think anybody is going to be confident they know what you're asking based on the first revision of your "question," I hope the following statement will give you at least some form of direction with how to achieve your... goal, I guess.  Here is an SQL Fiddle in case you would like to see the query in action.  I can only assume you'll also be interested in learning about COALESCE, as illustrated in this SQL Fiddle.

SELECT  *
FROM    `UserPost` up
LEFT JOIN `User` u
    ON  up.shared_id = u.id
    AND up.share_in = 1
LEFT JOIN `Group` g
    ON  up.shared_id = g.id
WHERE   up.user_id = 1;