Use LENGTH() for checking length in bytes:
SELECT str FROM sometable WHERE LENGTH(str) = 5;
Or CHAR_LENGTH() for checking length in number of characters (useful for multi-byte strings):
SELECT str FROM sometable WHERE CHAR_LENGTH(str) = 5;
سؤال
I'm using cakephp 1.2, and I have this situation:
I have a table which has a 'class' field, where class can be 'A','B','C','G', or a string of three letters 'DBB','ABA', etc.
Every string has sense (for example, A = Article, B = Book, etc).
I write this code:
if ($_GET['classificazione']!="art0")
{
$classificazione=$_GET['classificazione'];
if ($classificazione=="A"||$classificazione=="B"||$classificazione=="C"||$classificazione=="G")
$conditions[]=array('classe =' => "$classificazione");
This is working for the 'one letter' case, but I don't know what to do with 2 or more letters.
Since I don't care if they are equals ('DBB' and 'ABA' are both documents), I was thinking to check the length so I tried this:
else
$conditions[]=array('LENGTH(classe) > 1'); }
I tested the else, and now it's working.
المحلول
Use LENGTH() for checking length in bytes:
SELECT str FROM sometable WHERE LENGTH(str) = 5;
Or CHAR_LENGTH() for checking length in number of characters (useful for multi-byte strings):
SELECT str FROM sometable WHERE CHAR_LENGTH(str) = 5;