Complex Where Conditions

There are times when we want to display data based on multiple conditions placed on the database search. This example shows how to do just that.

Given a database table named "suppliers", we would like to load the table with a specific subset of records based on the following SQL statement:

SELECT suppliers.supplier_id,suppliers.supplier_name,suppliers.city,suppliers.state FROM suppliers WHERE (state = "California" AND supplier_id != 900) OR (supplieer_id = 100)

If you look under the Java tab, you'll see how we replicate these multiple criteria in our query.

First, we have in our database the following table of complete data:

supplier_id supplier_name city state
100MicrosoftRedmondWashington
200GoogleMountain ViewCalifornia
300OracleRedwood CityCalifornia
400Kimberly-ClarkIrvingTexas
500Tyson FoodsSpringdaleArkansas
600SC JohnsonRacineWisconsin
700Dole Food CompanyWestlake VillageCalifornia
800Flowers FoodsThomasvilleGeorgia
900Electronic ArtsRedwood CityCalifornia

What displays in our live example is the subset of records based on our query. This query returns all suppliers that are in the state of California but do not have a supplier_id equal to 900. The query will also return all suppliers whose supplier_id is equal to 100, which happens to be Microsoft. The parentheses determine the order that the AND and OR conditions are evaluated.

Live example


supplier_id supplier_name city state
Loading data from server
supplier_id supplier_name city state
Javascript
HTML
Java
Includes