I’ve got the three tables below from which I wish to get the following selects:
1.) Select all items from collaboration and group them by group_id in group_members for a given user
This means that when the user logs in, (s)he will see all (and only) collaboration items belonging to the groups where he is group member.
2.) For each group_id, select all collaboration items.
This means that when a user selects any group (group_id) from 1 above, he’s going to see all the collaborationitems belonging to the selected group (group_id)
Constraints: Each user MUST be a group member. The userstable is there to supply firstnameand lastname of user.
This is what I tried for 1 to no avail!
function OrderByGroup_id($username) {
$data = array();
$currenttime = time();
$q = "
SELECT *
FROM collaboration
INNER JOIN group_members ON collaboration.group_id = group_members.group_id
INNER JOIN users ON users.username = group_members.username
WHERE collaboration.parent_id IS NULL and collaboration.is_comment = 0
AND group_members.username = :user group by collaboration.group_id";
$sq = $this->connection->prepare($q);
$sq->execute(array(':user' => $username));
while($row = $sq->fetch()) {
$json = array();
$json['title'] = $row['title'];
$json['question'] = $row['content'];
$json['firstname'] = $row['firstname'];
$json['lastname'] = $row['lastname'];
$json['timestamp'] = $row['timestamp'];
$json['key'] = $row['group_id'];
$data[] = $json;
}
$allposts =json_encode($data);
return $allposts= json_decode($allposts, true);
}
Here are the tables
CREATE TABLE IF NOT EXISTS `collaboration` ( `id` int(11) NOT NULL AUTO_INCREMENT, `parent_id` int(11) DEFAULT NULL, `group_id` varchar(255) DEFAULT NULL, `author` varchar(30) NOT NULL, `title` varchar(255) DEFAULT NULL, `content` text NOT NULL, `is_comment` tinyint(1) unsigned NOT NULL, `file` tinyint(1) unsigned NOT NULL DEFAULT '0', `points` int(11) DEFAULT NULL, `timestamp` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `users` ( `firstname` varchar(30) NOT NULL, `lastname` varchar(30) NOT NULL, `username` varchar(30) NOT NULL, PRIMARY KEY (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `group_members` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(30) NOT NULL, `group_id` varchar(50) NOT NULL, `status` tinyint(1) unsigned NOT NULL, `timestamp` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Thanks for your input!
The following select does deliver the request for question 1. However, I know the query is not optimal because I do select entries that I don’t need at the moment. For example the use of *
I would like to select only
title,content,timestamp,group_id, from collaboration andfirstname,lastname, from usersSELECT * FROM collaboration INNER JOIN group_members ON collaboration.group_id = group_members.group_id INNER JOIN users ON users.username = collaboration.author WHERE collaboration.parent_id IS NULL and collaboration.is_comment = 0 AND group_members.username = :user GROUP BY collaboration.group_id ORDER BY collaboration.timestamp DESC