gpt4 book ai didi

php - 如何从三个表中获取没有重复行的记录?

转载 作者:行者123 更新时间:2023-11-29 16:58:18 27 4
gpt4 key购买 nike

如何从三个表中获取没有重复行的记录?(编辑问题) 下面是我的表结构

--
-- 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))
{

最佳答案

对“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

关于php - 如何从三个表中获取没有重复行的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52419062/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com