开发者

select from one mysql table where ids match results from select of another mysql table

I know I can accomplish this in PHP but I really need to have it in one single query for easier pagination. I have开发者_高级运维 2 tables as follows:

S_MATTERS

id | name
---------------------
 0 | Client 1
 1 | Client 2
 2 | Client 3

S_LINKS

mid | uid
---------------------
 2  | 0007
 0  | 0007

So I want to

select * from s_matters where id = (select mid from s_links where uid = 0007)

Obviously that is not the right syntax, I just need to get all the client names from the s_matters table where the uid is 0007 on the other table (id on s_matters = mid on s_links).


select m.*
    from s_matters m
        inner join s_links l
            on m.id = l.mid
    where l.uid = '0007'


Try changing the "=" to an "in"

 select * from s_matters where id in (select mid from s_links where uid = 0007)


SELECT * FROM s_matters JOIN s_links ON ( s_matters.id = s_links.mid ) WHERE s_links.uid = '0007'


An inner join is what you're looking for

select * from s_matters 
  inner join s_links on s_links.mid = s_matters.id 
where s_links.uid = '0007'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜