Jump to content

Selecting columns beginning with a numeric character


Pmzine

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?
Link to comment
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.
Link to comment
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?
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.