Naterade
Naterade

Reputation: 2675

Mysql LEFT to match first 3 chars

Im trying to get all matching records from the invoice_id field where the first 3 characters are RBK, case sensitivity not important. I've tried to use the LEFT function in the bottom 2 ways but its not working. Any ideas on how to achieve this?

SELECT *, IF( LEFT( invoice_id, 3) = 'RBK') FROM `invoices` ORDER BY id ASC

SELECT *, IF( LEFT( invoice_id, 3) = 'RBK', 3, 0) FROM `invoices` ORDER BY id ASC

Upvotes: 2

Views: 49

Answers (1)

Gal Sisso
Gal Sisso

Reputation: 1959

an if inside the select is not to filter results,if you want to filter result use where clause.

SELECT * FROM `invoices` WHERE LEFT(invoice_id, 3) = "RBK" ORDER BY id ASC

Upvotes: 2

Related Questions