php select from mysql where title begins with A (and A alone)

SoulieBaby picture SoulieBaby · Jan 15, 2010 · Viewed 39.4k times · Source

I'm sure this is super easy, but can't seem to figure it out.. I need to select all titles from my database where the title starts with A, or B, or C etc. Here's what I've tried so far:

SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A'

but returns nothing.. Could someone help me out with this?

Cheers

Answer

Tyler Carter picture Tyler Carter · Jan 15, 2010

For titles starting in 'A' use a % after the A

SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A%'

For titles with the letter 'A' in it, use % on either side of A

SELECT * FROM weblinks WHERE catid = 4 AND title LIKE '%A%'

For titles ending in the letter 'A', use % before the A

SELECT * FROM weblinks WHERE catid = 4 AND title LIKE '%A'

Basically % is a wildcard. It tells MySQL that anything can be in the location.

For having numbers as the first letter, check out Mark's answer.