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
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.
The wildcards for LIKE
are %
and _
, where % matches 0 or more characters and _ matches exactly one character.
The existing answers are correct for beginning with A:
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A%'
For beginning with any number you can use the REGEXP operator:
SELECT * FROM weblinks WHERE catid = 4 AND title REGEXP '^[0-9]'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With