Jump to content


Photo

**SOLVED** MULTIPLE ON-WHERE WITH A SINGLE JOIN IS ALLOWED ?


  • Please log in to reply
4 replies to this topic

#1 swatisonee

swatisonee
  • Members
  • PipPipPip
  • Advanced Member
  • 253 posts

Posted 18 March 2006 - 05:34 PM

Hi,

I'm finding my way thru JOINS and have this single statement to execute. It doesnt work because its syntactically wrong. I'm not able to figure out how I should change it and would appreciate some guidance. Thanks.

$sql = "SELECT `Enq`.* FROM `Enq` LEFT JOIN `Budget` 

ON `Enq`.Enquiryid=`Budget`.Enquiryid WHERE `Budget`.Enquiryid IS NULL 

AND ON  `Enq`.Visitid =`Budget`.Visitid WHERE `Budget`.Visitid IS NULL 

// syntactically wrong 


AND ( (`Enq`.`Visitid`= '0' || `Enq`.`Visitid` IS NULL) 

AND  `Enq`.Category!= 'D' AND `Enq`.Category!= 'A' ) 

and so on and so forth....


Basically, if the field Visitid has a value in Enq but does not appear in a record in Budget, it should get selected.
Also, all other records in Enq where Visitid is 0 or null should also get selected.
This is because, there are records in Budget with value in the Visitid field that correspond to a value in Enq and those should not be selected.
Shishya

#2 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 18 March 2006 - 06:33 PM

select * from enq e
left join budget b on b.enquiryid = e.enquiryid 
and b.visitid = e.visitid

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#3 swatisonee

swatisonee
  • Members
  • PipPipPip
  • Advanced Member
  • 253 posts

Posted 19 March 2006 - 03:25 AM

Hi, thanks for replying but that was the first option i had tried but it didnt return any records.

I hope i can explain correctly :

a. I have 2 table that need to be joined.

b. I have 3 types of data to select from between these 2 tables.
* all records from Enq which do not find a match in Budget
* all records from Enq where the field visitid has null value which do not find a match in Budget
* all records from Enq where the field visitid has a value but which do not find a match in Budget
* all record in Enq which have values in both enquiryid and visitid but does not find a match in Budget

Records have to be scanned to check that both the enquiryid and visitid fields of Budget return null values. My problem is the second and third have records which can be subsets of the first.

The field visitid comes from a third table called Visit.

Any suggestions pl? Thanks.
Shishya

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 March 2006 - 09:05 AM

Perhaps you could post the structure of these three tables with some test data plus the predicted result from the query. This would greatly assist our understanding of the problem.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 swatisonee

swatisonee
  • Members
  • PipPipPip
  • Advanced Member
  • 253 posts

Posted 20 March 2006 - 04:32 AM

Resolved using Union to select 2 separate sql statements.
Shishya




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users