How to select data from two tables using a single query

前端 未结 5 873
北海茫月
北海茫月 2021-01-29 02:02

I\'ve been trying to get my head around how to use a single query to select data from two of my tables. If anybody can suggest a better way than a single query, I\'m all ears! P

相关标签:
5条回答
  • 2021-01-29 02:24
    select
       *
    from
       member as m
       left outer join
          networking as n
       on
          m.networkingID=n.ID
    
    0 讨论(0)
  • 2021-01-29 02:38
    SELECT * FROM member
    LEFT JOIN networking
    ON member.networkingID=networking.ID
    WHERE member.ID=2
    
    0 讨论(0)
  • 2021-01-29 02:42
    select * from member a, networking b 
    where a.networkingID=b.ID and a.ID = 2
    
    0 讨论(0)
  • 2021-01-29 02:44

    Simple join, between a common id. Inner join will ensure there are records in the networking table, otherwise it won't show that member. you can replace it with a LEFT JOIN if you want all the member rows regardless if they have anything joined in the network table

     SELECT * FROM member m 
          INNER JOIN networking n 
     ON (m.networkingID = n.id) 
     WHERE m.id = 2;
    
    0 讨论(0)
  • 2021-01-29 02:45

    A JOIN should work.

    SELECT * FROM member, networking WHERE member.ID=2 AND member.networkingID=networking.ID
    

    This will return an empty result if there's no networking data for member.ID=2. If you want to get a result in this case, you can try LEFT JOIN.

    SELECT * FROM member LEFT JOIN networking ON member.networkingID=networking.ID WHERE member.ID=2
    
    0 讨论(0)
提交回复
热议问题