Jump to content

Query with JOINS


imperium2335

Recommended Posts

Hi,

 

I'm trying to a query to check whether a record has a reference in two other tables, I have the following:

 

Explain SELECT COUNT(*) AS new FROM entity_details
						 	LEFT JOIN entity_turned_prospect
							ON entity_details.id = entity_turned_prospect.entityRef
							LEFT JOIN entity_turned_customer
							ON entity_details.id = entity_turned_customer.entityRef
						 WHERE entity_details.ownerRef = 41
		  				 AND entity_details.typeRef = 4
						 AND entity_details.dateCreated = MONTH(NOW())
		  				 AND entity_turned_prospect.id = NULL
						 AND entity_turned_customer.id = NULL

 

But I get 0 results returned when I know for a fact there should be some.

 

If I change the count to just select * I then get an error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS new FROM entity_details, LEFT JOIN entity_turned_prospect ON' at line 1

 

Could someone show me how to fix this?

Link to comment
Share on other sites

Sorry, it should be:

 

Explain SELECT COUNT(*) FROM entity_details
						 	LEFT JOIN entity_turned_prospect
							ON entity_details.id = entity_turned_prospect.entityRef
							LEFT JOIN entity_turned_customer
							ON entity_details.id = entity_turned_customer.entityRef
						 WHERE entity_details.ownerRef = 41
		  				 AND entity_details.typeRef = 4
						 AND MONTH(entity_details.dateCreated) = MONTH(NOW())
		  				 AND entity_turned_prospect.id = NULL
						 AND entity_turned_customer.id = NULL

Link to comment
Share on other sites

I tried that but that still gives me the same column names as the main table.

 

I.e. my table has 3 columns called id and 2 columns called entityRef.

 

It is the lines :

 

entity_turned_prospect.id = NULL

AND entity_turned_customer.id = NULL

 

That are causing no results.

 

I want these statements to only apply to the joined tables and not the main table, because according to these two lines those columns will never be null.

 

Do you know what I mean?

Link to comment
Share on other sites

I've just tried:

 

SELECT
(SELECT entityRef AS a1 FROM entity_turned_prospect) AS t1,
(SELECT entityRef AS b1 FROM entity_turned_customer) AS t2
FROM entity_details
LEFT JOIN t1
ON entity_details.id = t1.a1
LEFT JOIN t2
ON entity_details.id = t2.b1
WHERE entity_details.ownerRef = 41
AND entity_details.typeRef = 4
AND MONTH(entity_details.dateCreated) = MONTH(NOW())
#AND entity_turned_prospect.id = NULL	
#AND entity_turned_customer.id = NULL

 

But tells me that table t1 doesn't exists.

 

I feel this is closer to what I want just need to understand why this isn't working now.

Link to comment
Share on other sites

Thanks for your help.

 

I just tried a different method and it has worked.

 

SELECT entity_details.id
						FROM entity_details
						WHERE entity_details.id NOT IN (SELECT entityRef FROM entity_turned_prospect)
						AND entity_details.id NOT IN (SELECT entityRef FROM entity_turned_customer)
						AND MONTH(entity_details.dateCreated) = MONTH(NOW())
						AND entity_details.typeRef = 4
						AND entity_details.ownerRef = ?

 

Thanks for your insights!

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.