如何从三个表中提取记录而不产生重复行?

0g0grzrc  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(248)

这个问题在这里已经有答案了

mysql-如何连接两个没有重复的表(5个答案)
两年前关门了。
如何从三个表中提取记录而不重复行?(编辑问题)下面是我的表结构

--
-- table structure for table `users`
--
CREATE TABLE `users`(
`user_id` int(11)NOT NULL,
`name` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(300) NOT NULL,
`time_joined` time NOT NULL,
`date_joined` date NOT NULL
)ENGINE=MyISAM  DEFAULT CHARSET=latin1;
--
-- table structure for table `activity`
--
CREATE TABLE `activity`(
`activity_id` int(11)NOT NULL,
`user_id` int(11) NOT NULL,
`time_loged` time NOT NULL,
`time_out` time NOT NULL
)ENGINE=MyISAM  DEFAULT CHARSET=latin1;
--
-- table structure for table `timeout`
--
CREATE TABLE `timeout`(
`timeout_id` int(11)NOT NULL,
`user_id` int(11)NOT NULL,
`time_out` time NOT NULL
)ENGINE=MyISAM  DEFAULT CHARSET=latin1;

这是我的努力

$id=$_SESSION['user'];
        $query = $conn->query("SELECT *  FROM users left join timeout on users.user_id=timeout.user_id left join activity on users.user_id=activity.user_id WHERE users.user_id='$id'");
    while($row = $query->fetch(PDO::FETCH_ASSOC))
    {
ddrv8njm

ddrv8njm1#

对“activity”和“timeout”执行第二个左连接,因此查询应如下所示:

SELECT * FROM users
LEFT JOIN timeout ON users.user_id = timeout.user_id
LEFT JOIN activity ON timeout.user_id = activity.user_id
WHERE users.user_id = '$id'

你可以在这里得到进一步的解释:https://www.codeproject.com/questions/693539/left-join-in-multiple-tables

相关问题