如何使用mysql从这三个表中提取所需的数据?

时间:2017-08-18 14:04:05

标签: mysql

很抱歉,我正在努力找出正确的SQL来从这三个表中提取所需的数据......

users:- id, firstname, lastname, email, live
contracts:- id, userid, contractid
userstage:- id, userid, stageid, statusid

我需要来自用户的名字,姓氏,电子邮件,直播 并从合同中收缩, 和来自userstage的statusid

WHERE users.live =1 AND contracts.contractid = 1 AND userstage.statusid <>4 (not equal to 4)

谢谢。

2 个答案:

答案 0 :(得分:2)

这是非常基本的SQL。别忘了你的问题是什么。您可能希望根据需要更改为OUTER JOIN(左或右)。

SELECT u.firstname, u.lastname, u.email, u.live, c.contractid, us.statusid
FROM users AS u
INNER JOIN contracts AS c ON u.id = c.userid
INNER JOIN userstage AS us ON u.id = us.userid
WHERE u.live = 1 AND c.contractid = 1 AND us.statusid <> 4

答案 1 :(得分:1)

根据您在某些用户没有合同等时的操作,您可以从:

开始
SELECT  firstname, lastname, email, live, contractid, statusid 
FROM users
JOIN contracts ON contracts.userid = users.id
JOIN userstage ON userstage.userid = users.id
WHERE users.live =1 AND contracts.contractid = 1 AND userstage.statusid <>4 (not equal to 4)