Jump to content

Count results in left join?


Go to solution Solved by Barand,

Recommended Posts

Hi guys, trying to make the move to MySQLi and some of it is baffling me. Quick question as my query is currently saying there's 1 result even if there isn't one...

 

How do I go about finding the number of rows returned for this:

$sql = <<<SQL
    SELECT u.*, t.*
    FROM Users u
	LEFT JOIN Transactions t USING (UserID) 
	WHERE UserID = $UserID
	ORDER BY Date DESC
	LIMIT 5
SQL;

Thanks in advanced!

Link to comment
https://forums.phpfreaks.com/topic/285501-count-results-in-left-join/
Share on other sites

 

$row_cnt = mysqli_num_rows($result);
echo $row_cnt;
while($row = $result->fetch_array()) { 
}

It's echoing number 1 and also showing 1 row with blank values and 01/01/1970 as the date.

 

 

So, it is echoing 1, meaning there was one record in the result. Then the while loop, apparently, is displaying the values of one row where most values are null and a data value of '01/01/1970'. I'm not seeing the problem. The fact that some of the values in the record that is returned are NULL doesn't mean there wasn't a record in the result. You must be leaving out some important information here.

There's no transactions currently, but there are users, but there can't be any transactions by the logged in user as there are no transactions at all. Sorry, when I said no rows in the table I meant no rows related to the query, although there are no rows in transactions.

Edited by Kristoff1875

Ok, I think I understand why it's returning 1 row, because it's seeing that the user exists, but not any transactions. So the result does exist, but the transactions don't... Gazumped now though as I don't know how to check for results that match both.

  • Solution

A LEFT JOIN B ---> Returns all records in A with data from B where there is a match, otherwise null values from B

A INNER JOIN B ---> Ony returns rows where there is matching data in both A and B

By the way, if you use COUNT() function with a column name like count(colname) as a single argument, mysql counts the rows only with non-null values and returns 0 (zero) if the value of the column is null. However, if you use count(*) mysql returns the total number of rows in the result set.

Edited by jazzman1

A LEFT JOIN B ---> Returns all records in A with data from B where there is a match, otherwise null values from B

A INNER JOIN B ---> Ony returns rows where there is matching data in both A and B

Perfect thank you, you're a star. Amazing how something so simple can throw you off in such a way!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.