0

How can i fetch record from three table without duplicate rows?(Edited question) below is my table structure

--
-- 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;

And here is my effort

$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))
    {
Clic Here
  • 1
  • 2

1 Answers1

0

Do the second left join on 'activity' and 'timeout', so the query should look like this:

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'

You can get a further explanation here: https://www.codeproject.com/Questions/693539/left-join-in-multiple-tables

falzberger
  • 78
  • 7