Reputation: 51
I have one user table and one table that stores the records created by the users. I wrote a query to fetch the user accounts from the user table and display it to the web admin for account management. I want to populate another column that will show the number of records created by each user that have been stored in the second table.
I wrote two separate queries. One helps to fetch the user accounts and the other query fetches the number of records created by each user.
How can I merge these two queries together; and have one table instead of two and have the query populate the number of records created by the user in the second db table right next to the user account info that has been pulled in from db table 1.
$sql = "SELECT user_id, fullname, username, email FROM users";
$sql2 = "SELECT username, COUNT(id) AS id_count
FROM table2
GROUP BY username";
if($result = $mysqli->query($sql)){
if($result->num_rows > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>#</th>";
echo "<th>Full Name</th>";
echo "<th>Username</th>";
echo "<th>Email</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch_array()){
echo "<tr>";
echo "<td>" . $row['user_id'] . "</td>";
echo "<td>" . $row['fullname'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
$result->free();
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
if($result = $mysqli->query($sql2)){
if($result->num_rows > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>Username</th>";
echo "<th>Records Per Account</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch_array()){
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['id_count'] . "</td>";
}
echo "</tbody>";
echo "</table>";
// Free result set
$result->free();
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
$mysqli->close();
?>
Upvotes: 0
Views: 82
Reputation: 16688
You could use a subquery like this:
SELECT
T1.user_id,
T1.fullname,
T1.username,
T1.email,
(SELECT COUNT(T2.id) FROM table2 AS T2 WHERE T1.username = T2.username) AS id_count
FROM
users AS T1
However, I would use user_id
as the foreign key in table2
, not username
. The obvious reason is that there may could two users with the same name. The last thing you should do is to report to a new user: "Sorry, that username already exists".
Make sure your foreign key is properly indexed.
Upvotes: 1
Reputation: 78105
You need to form a JOIN between the two tables, and group by all the fields you need, apart from the aggregate function - which is COUNT in this case.
Something like this might work (I can't test it):
SELECT
u.user_id,
u.fullname,
u.username,
u.email,
COUNT(*)
FROM users u
INNER JOIN table2 t2
ON t2.username = u.username
GROUP BY
u.user_id, u.fullname, u.username, u.email
But, more than likely joining on user_id will be better - it depends on what columns and indexes the tables have. More generally: suggest you look into some SQL concepts to widen your understanding.
Upvotes: 2