I've got three tables: bands, gigs, and venues. I want to be able to search by band name, band hometown, and venue name. Here's the general idea of what my tables look like:
bands
ID | NAME | LOCATION
------|--------------|------------
1 | Spinal Tap | New York, NY
2 | Jimi Hendrix | Woodstock, PA
gigs
ID | VENUE_ID | START_TIME | BAND_ID
------|----------------|------------------|--------------
1 | 1 | 1371171600 | 1
2 | 2 | 1371171600 | 1
3 | 1 | 1371171600 | 2
4 | 2 | 1371171600 | 1
venues
ID | NAME
------|---------------------
1 | Madison Square Garden
2 | Jefferson Round Garden
So searching by band name and location is easy. Something like:
SELECT id,name,location
FROM bands
WHERE name LIKE '%$search_string%' OR location LIKE '%$search_string%'
ORDER BY name
What if I want to include the possibility of searching by venue name? Here's my horribly botched attempt:
SELECT bands.id,bands.name,bands.location,venues.name
FROM bands
WHERE name LIKE '%$search_string%' OR location LIKE '%$search_string%'
INNER JOIN gigs
ON bands.id=gigs.band_id
INNER JOIN venues
ON gigs.venue_id=venues.id
WHERE start_time>'$now'
ORDER BY bands.name
I'm obviously new to this whole inner join thing...