erewash Posted June 4, 2006 Share Posted June 4, 2006 Forgive me re-posting, but I guess nobody will look at a problem that has been marked as 'solved' - Barand gave me a very helpful answer but it does not fix the central problem so I will try to explain better.I have a people table 'ocp_people', and an address table 'ocp_address', and a table containing lots of 'event participation' codes 'ocp_people_prog'. Each individual should have one address (but not always), and may have lots of event codes.As modified by Barand's answer it reads something like this;[code]SELECT DISTINCT pe.people_id, pe.surname, pe.forenameFROM (ocp_people AS pe LEFT JOIN ocp_address AS addr ON pe.people_id=addr.people_id)LEFT JOIN ocp_people_prog AS lkON pe.people_id = lk.ppeople_idWHERE pe.people_id <> ''AND ( (lk.pprog_id LIKE 'code1%')OR (lk.pprog_id LIKE 'code2%' ) )GROUP BY pe.people_id ORDER BY pe.surname [/code](I have to generate the boolean operators with a drop-down for the user, which is fiddly but works)This is fine where I want everyone who fits into either category; but how about if I want to collect only those where both criteria apply - and I might be wanting to search more than two codes as here. Thus in [u]pseudocode[/u];[code]AND lk.pprog_id LIKE 'code1%'AND lk.pprog_id LIKE 'code2%'AND lk.pprog_id NOT LIKE 'code3%'[/code]How is it possible to achieve this within a single statement? I can't see any way of achieving the logic of this statement without applying multiple 'AND's. Any assistance gratefully received..... Link to comment https://forums.phpfreaks.com/topic/11173-running-selects-against-lookup-tables/ Share on other sites More sharing options...
.josh Posted June 4, 2006 Share Posted June 4, 2006 umm.. just change the OR to an AND..? Link to comment https://forums.phpfreaks.com/topic/11173-running-selects-against-lookup-tables/#findComment-41815 Share on other sites More sharing options...
erewash Posted June 4, 2006 Author Share Posted June 4, 2006 No - Barand's original post to my original question demonstrated that this cannot work - the result is always an empty set, as I was finding with my code in the first place![!--quoteo(post=379989:date=Jun 4 2006, 01:38 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 4 2006, 01:38 PM) [snapback]379989[/snapback][/div][div class=\'quotemain\'][!--quotec--]umm.. just change the OR to an AND..?[/quote] Link to comment https://forums.phpfreaks.com/topic/11173-running-selects-against-lookup-tables/#findComment-41823 Share on other sites More sharing options...
Barand Posted June 5, 2006 Share Posted June 5, 2006 [code]SELECT pe.people_id, pe.surname, pe.forename, COUNT(DISTINCT lk.pprog_id) as codecountFROM (ocp_people AS pe LEFT JOIN ocp_address AS addr ON pe.people_id=addr.people_id)LEFT JOIN ocp_people_prog AS lkON pe.people_id = lk.ppeople_idWHERE pe.people_id <> ''AND ( (lk.pprog_id LIKE 'code1%')OR (lk.pprog_id LIKE 'code2%' ) )GROUP BY pe.people_idHAVING codecount = 2 ORDER BY pe.surname[/code]Although with your peculiar coding structure I don't guarantee it - would be ok if you separated code parts as previously recommended Link to comment https://forums.phpfreaks.com/topic/11173-running-selects-against-lookup-tables/#findComment-42225 Share on other sites More sharing options...
erewash Posted June 6, 2006 Author Share Posted June 6, 2006 Barand,Many thanks for your help on this. Looks like I will have to do a major bit of restructuring. Cannot believe how difficult it is to sort this out.... Link to comment https://forums.phpfreaks.com/topic/11173-running-selects-against-lookup-tables/#findComment-42479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.