title: Using IF … THEN … ELSE to choose between two SELECT queries
body: <p>I need help with the SELECT statement in an application that I will call 'app_Update_Users'. This application is designed to update records in a Users table. The application code makes reference to the following tables:  </p>
  
  <ul>
  <li>States  </li>
  <li>Counties</li>
  <li>Organizations</li>
  <li>Users</li>
  </ul>
  
  <p>The States table contains a list of all 50 US states. The Counties table contains a list of the 58 counties in the state of California. The Organization table contains a list of organizations that may be located in any state and that provide services in one or more California counties. When adding or updating a User record, I want to filter the list of Organizations based on the state in which they are located and the county or counties in which they provide services.</p>
  
  <p>The application has three select-type statements to create picklists of attributes in a Users record. I have written code that works for the first two statements, but I am having problems with the code for the third statement. The fields referenced by these statements are: 
   - uState
   - uCounty
   - uOrganization</p>
  
  <p>The statements for the uState and uCounty fields invoke onChange Ajax processing to update the selections on the picklist for the uOrganization field. The options for the picklists for first two fields are selected with the following code:</p>
  
  <p>For the uState field:</p>
  
  <pre><code>SELECT stAbbr, CONCAT(stAbbr, ' - ', stStatePK)
     FROM States 
     ORDER BY stAbbr;
  </code></pre>
  
  <p>For the uCounty field:</p>
  
  <pre><code>SELECT cCounty, cCounty 
     FROM Counties 
     ORDER BY cCounty;
  </code></pre>
  
  <p>The records in the Organizations table can have multiple counties listed in the oCounties field because an organization can operate in multiple counties. Therefore the options for the picklist for the uOrganization field are currently selected with the following code: </p>
  
  <pre><code>SELECT oName
     FROM Organizations 
     WHERE oCounties LIKE '%{uCounty}%' 
     ORDER BY oName ASC ;  
  </code></pre>
  
  <p>I can also select records in the Organizations table based on the state in which they are located with the following code: </p>
  
  <pre><code>SELECT oName, oName
     FROM Organizations 
     WHERE oState = '{uState}' 
     ORDER BY oName ASC ;
  </code></pre>
  
  <p>However, the logic that I want to use for selecting the options for the uOrganization field would combine these two code snippets. In plain English, it would go something like this: "If the uState field is set to 'California', then select records from the Organizations table based on the value in the uCounty field. IF the uState field is not set to 'California', then select records from the Organizations table based on the value in the uState field. </p>
  
  <p>The following IF . . . THEN . . . ELSE syntax gets the point across, but it doesn't work:</p>
  
  <pre><code>IF uState = 'CA' THEN
     SELECT oName, oName
     FROM Organizations 
     WHERE oCounties LIKE '%{uCounty}%' 
     ORDER BY oName ASC ;  
  ELSE
     SELECT oName, oName
     FROM Organizations 
     WHERE oState = '{uState}' 
     ORDER BY oName ASC ;
  ENDIF
  </code></pre>
  
  <p>What is the correct way to write this code? </p>