Jump to content

Archived

This topic is now archived and is closed to further replies.

Pmzine

Selecting columns beginning with a numeric character

Recommended Posts

Hey,

Hopefully a quick and easy question, I have a MYSQL database with reviews in it and an A-Z archive. For instance, if you click 'A' link it will show all reviews beginning with the letter 'A'.

I want to be able to grab the reviews beginning with a numeric value though and can't figure out the character to pass to the SQL statement.

Any ideas?

Share this post


Link to post
Share on other sites
[!--quoteo(post=376726:date=May 24 2006, 11:31 AM:name=Pmzine)--][div class=\'quotetop\']QUOTE(Pmzine @ May 24 2006, 11:31 AM) [snapback]376726[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hey,

Hopefully a quick and easy question, I have a MYSQL database with reviews in it and an A-Z archive. For instance, if you click 'A' link it will show all reviews beginning with the letter 'A'.

I want to be able to grab the reviews beginning with a numeric value though and can't figure out the character to pass to the SQL statement.

Any ideas?
[/quote]

Pass in a # sign and then do an If statement for each number.

Share this post


Link to post
Share on other sites
[code].... WHERE SUBSTRING(review, 1,1) IN ('0','1','2','3','4','5','6','7','8','9')
[/code]

Share this post


Link to post
Share on other sites
This is the SQL statement I'm using so far:

[code]$sql = "select * from cdreviews ";

if ($_GET['sort_by']) {
  $letter = $_GET['sort_by'];
  $sql .= "where name like '".$letter."%' ORDER BY name ASC";
} else {
   $sql .= "order by dateadded DESC limit 10";
}[/code]

How would I integrate that statement into it?

Share this post


Link to post
Share on other sites
Something like

[code]$sql = "select * from cdreviews ";

if ($_GET['sort_by']) {
  $letter = $_GET['sort_by'];
  if ($letter=='#') {
      $sql .= "WHERE SUBSTRING(name, 1,1) IN ('0','1','2','3','4','5','6','7','8','9') ORDER BY name ASC";
  }
  else {
      $sql .= "where name like '$letter%' ORDER BY name ASC";
  }
} else {
   $sql .= "order by dateadded DESC limit 10";
}[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.