mysql - How to append the output of subquery to the current main query? -


i have a: posttable:

it has varchar content, , int groupid


and joinedgroupstable:

it has int userid , int groupid


whenever user joins group, record added groupid has userid , groupid.

i want send database query posts posttable has groupid user has joined.

this i'm trying do:

select  posttable.content posttable, joinedgroupstable posttable.groupid = (select group_concat(joinedgroupstable.groupid separator " or posttable.groupid =")  joinedgroupstable  joinedgroupstable.userid=1 ) 

this example code , doesn't work yet, because of last part between parentheses.

this part:

(select group_concat(joinedgroupstable.groupid separator " or posttable.groupid =")      joinedgroupstable      joinedgroupstable.userid=1 ) 

will output "1 or posttable.groupid =2 or posttable.groupid =3" etc. depending on groups id adjacent user in joinedgroupstable, want join output main query becomes this:

select  posttable.content posttable, joinedgroupstable posttable.groupid = 1  or posttable.groupid =2 or posttable.groupid =3 

and joining output of subquery main query. don't know if possible, , know if there simpler way so.

maybe you're after... don't see why need join joinedgroupstable eliminated join. please use inner, left, right join notation instead of , notation in clause.

this approach can slow @ times , doesn't work @ when groupid null in 1 or both cases (especially in not in situations)

select posttable.content posttable posttable.groupid in (select groupid                              joinedgroupstable                              userid=1 ) 

an alternative way join tables limit. works if need data both tables.

select posttable.content posttable inner join joinedgroupstable   on posttable.groupid = joinedgroupstable.groupid  userid=1  

or using coorlated query , exists.

this handles nulls when using not in or not exists.

select pt.content posttable pt exists (select *               joinedgroupstable jgt               userid=1                  , pt.groupid = jgt.groupid) 

each has own strengths , weaknesses in different engines , vary in performance based on data. have test find works best you; 3 should produce same results (except in null situation)


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -