skip to Main Content

I have faced query with MySQL. I need to view all record by using Alphabetic order.

Here is my code –

$alphabetsArr = array('#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

foreach($alphabetsArr as $alpha) {
        $conditions = array('CreateListing.is_complete' => 1, 'BuilderType.builder_type LIKE'=>$alpha.'%');
        $alphabets[$alpha] = $this->CreateListing->find('all', array('conditions' => $conditions, 'group' => array('CreateListing.builder'), 'contain' => array('BuilderType'), 'fields' => $fields, 'order' => $order, 'limit' => 50));
       //$alphabets[$alpha] = mysql_fetch_array(mysql_query("SELECT * FROM create_listings WHERE name LIKE '".$alpha."%'"));
 }


<?php
    foreach ($alphabets as $key => $letter) {
        if (count($letter)) {
        ?>
        <div class="make-group"><a id="<?php echo $key; ?>-make"></a><h3><?php echo $key; ?></h3>
            <div class="make-list columnizer-3">
                <?php foreach ($letter as $brandname) { ?>
                <div><a href="<?php echo $this->webroot.'listing/brand:'.$brandname['BuilderType']['seo'];?>"><?php echo $brandname['BuilderType']['builder_type']; ?></a></div>
    <?php } ?>
                 </div>
                 <br class="clear">
            </div>
        <?php }
    }
 ?>

My code is successfully working. But in case i am getting one problem while search non-alphabetic text. there have # so I want to show all non-alphabetic text which starting with Number or any Symobl or Special character

How to search that text except A-Z in single query?

NB – This is CakePHP code. So don’t worry about this code. I need query. If you know core php then you can post your answer by using core php

2

Answers


  1. You can use RLIKE with negation NOT,it will match name column and return rows which doesn’t starts with a to z all alphabets

    SELECT
      *
    FROM
      create_listings
      WHERE name NOT RLIKE '^[a-z]' 
    

    DEMO

    Login or Signup to reply.
  2. you can also try this

    SELECT * FROM table WHERE first_name REGEXP '^[^A-Za-z]';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search