mysql - How do I SELECT data based on pairs of columns (themselves the result of a SELECT)? -


suppose have following table structure:

table session     int entity_id     int user_id     int status  table entity_user     int entity_id     int user_id     int target 

the session table logs interactions different users on different entities. entity_user table specifies users have access each entity. significantly, each user can have access more 1 entity.

i want select (entity, user) pairs session table based on criteria, eg. particular status. having retrieved pairs, want corresponding target in entity_user table each pair.

is there way cleanly in sql, ideally single query?

my solution far select pairs, offline text processing concatenate them (with separator) , use text. thus:

select entity_id, user_id session status = 100; -- returns (101, 234), (204, 157), etc. -- post-process result using # separator select entity_id, user_id, target entity_user   concat(user_id, '#', entity_id) in ( '101#234', '204#157', ...) 

this works, feel there should pure way in sql. suggestions?

can done combination of subquery , join.

select * (    select entity_id, user_id session status = 100 ) s left join entity_user on s.entity_id = entity_user.entity_id , s.user_id = entity_user.user_id 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -