Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select rows only where first digit is a number from 0 to 9?

Tags:

php

mysql

As far as I know I can do something like:

"SELECT * 
 FROM my_table 
 WHERE my_field LIKE '0%' 
 OR my_field LIKE '1%' 
 OR my_field LIKE '2%' ";

Is there a way to achieve this with a regular expression or something like this:

"SELECT * 
 FROM my_table 
 WHERE my_field LIKE [only first char is 0-9]"??

EDIT: The field is not numeric and it can be something like "1 People", "211 Pies" and so on.

like image 598
Uuid Avatar asked Mar 15 '13 20:03

Uuid


People also ask

How do you select nth row?

ROW_NUMBER (Window Function) ROW_NUMBER (Window Function) is a standard way of selecting the nth row of a table. It is supported by all the major databases like MySQL, SQL Server, Oracle, PostgreSQL, SQLite, etc.

How do I assign a default value if no rows returned from the select query MySQL?

Here's one way: SELECT *, IFNULL( ( SELECT col1 FROM table1 WHERE col1 IN ('012311','0123631','091233','092111') ), 'some_value' ) AS my_col1 FROM table1; Not neccessarily copy+paste, you will have to adjust for your specific case.


1 Answers

Try this

SELECT * 
FROM BadlyDesignedTable 
WHERE AnswerColumn RLIKE '^[0-9]+'

I was wondering if it was even possible to regex in where, found it on google in 30 seconds.

like image 162
Jack M. Avatar answered Sep 27 '22 20:09

Jack M.