简体   繁体   中英

Sql statement to join two table

Suppose I have two table

Table Name group

column 1: giver

column 2: acceptor

Table name userinfo

column 1: name

column 2: status

i want to select giver,acceptor and userinfo.status that from group table where giver or acceptor whose name is zakir that giver or acceptor exist in uerinfo table as name.

Need Help to write sql statement for taht query.. Thanks in advance... :)

What you are referring to is also known as the INNER JOIN clause in an SQL Statement.

Depending on the relationship you can create an INNER JOIN to potentially connect the two variables that are identical.

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

Extracted from W3Schools.com

Try the below query,

SELECT 
gp.giver,gp.acceptor,ui.status 
FROM group as gp JOIN userinfo as ui 
on ui.name = 'zakir' 
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')

Or try this without join,

SELECT 
gp.giver,gp.acceptor,ui.status 
FROM group as gp, userinfo as ui 
WHERE ui.name = 'zakir' 
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')

Try this:

SELECT `group`.`giver`, `group`.`acceptor`, `userinfo`.`status`
FROM `group`, `userinfo`
WHERE (`group`.`giver` = 'zakir' OR `group`.`acceptor` = 'zakir')
  AND `userinfo`.`name` = 'zakir'
select g.giver, g.acceptor u.status
from group g, userinfo u
where u.name = 'zakir'
and (g.giver = u.name or g.acceptor = u.name)

It should do a part of the job.

This assumes you want the status for both the giver and the acceptor

May need a little tweaking for mysql syntax

Select giver, g.status, acceptor, a.status 
FROM GROUP 
join userinfo as g on group.giver = g.name
join userinfo as a on group.acceptor = a.name
where (giver = 'zakir' or acceptor = 'zakir')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM