Jump to content

mysql returns the same results twice


Roee

Recommended Posts

Hi

I have the following SQL QUERY:

		$sql = "SELECT DISTINCT tbl1.*, tbl2.*
		FROM tbl1, tbl2
		WHERE tbl1.active='0'
			AND tbl2.userID='".$me['id']."' 
		AND (tbl1.site1ID = tbl2.siteID OR tbl1.site2ID = tbl2.siteID) 
		ORDER BY tbl2.success DESC 
		LIMIT 10";

 

The query workes well , but returns the results twice.

I mean that if the results are:

result1,result2,result3

 

I will get:

result1

result1

result2

result2

result3

result3

 

Why is it happening?

 

THANKS

Link to comment
Share on other sites

I'm guessing for each of the tables there is at least 1 field within each of the rows which are different.  Thus making the DISTINCT catch it as a unique value.

 

You will probably have to reduce the returned set (from * to something else) in order to remove the duplication.

 

Another thing I noticed is you are using a cross join between tbl1 and tbl2 (,) and then restricting it.  This may affect the speed of your query.  You may want to change it to something like this:

 

$sql = "SELECT DISTINCT tbl1.*, tbl2.*
FROM tbl1 a
JOIN   tbl2 b ON ( a.site1ID = b.siteID OR a.site2ID = b.siteID )
WHERE tbl1.active='0'
AND tbl2.userID='".$me['id']."' 
ORDER BY b.success DESC 
LIMIT 10";

 

to work better.

 

It won't fix your DISTINCT problem, but it may speed things up a bit ... You'll need to identify the key fields before you can get the DISTINCT to work as you wanted it to.

~juddster

Link to comment
Share on other sites

Another thing I noticed is you are using a cross join between tbl1 and tbl2 (,) and then restricting it.  This may affect the speed of your query.  You may want to change it to something like this:

[...]

 

Not really. For optimiser both queries are equivalent.

Link to comment
Share on other sites

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.