Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select where first character is number

Tags:

php

mysql

I'm trying to organize my content by the first character. By now, I did something like this:

PHP:

$first_char = $_GET['search'];
$sql = "SELECT id
        FROM table
        WHERE SUBSTR(title,1,1) = '".$first_char."'"
....

HTML:

<a href="?search=a">[A]</a>
<a href="?search=b">[B]</a>
...
<a href="?search=z">[Z]</a>
<a href="?search=nr">[#]</a>

The thing is that when [A] is clicked, it shows titles starting with "A". What I want to do is when [#] is clicked, it should show all the posts starting with a number. So, when $_GET = 'nr' it shows all the titles starting with a number... I tried to do this with array(), but I failed.

Do you have any suggestions?

like image 746
Cosmi Avatar asked Sep 19 '11 20:09

Cosmi


People also ask

How do you check if the first character of a string is a number SQL?

select ISNUMERIC(left('4ello world',1)) will be a "1" if the first character is a number.

How do I get the first character of a string in SQL Server?

SQL Server LEFT() Function The LEFT() function extracts a number of characters from a string (starting from left).

How do you select the first character of a string?

You should use the charAt() method, at index 0, to select the first character of the string. NOTE: charAt is preferable than using [ ] (bracket notation) as str.


1 Answers

$sql = "SELECT id
        FROM table
        WHERE SUBSTR(title,1,1) in (1,2,3,4,5,6,7,8,9,0)"
like image 82
Bluewind Avatar answered Sep 20 '22 23:09

Bluewind