2013-02-04 48 views
0

我想从数据库中按以下方式PHP MySQL的选择WHERE

SELECT title FROM video WHERE REPLACE(title, '-', title) < title='video 03' ; 

所以我想,以取代取“ - ”或任何其他符号,然后选择的结果,如果他们是小EXP的: '视频03'

现在,例如,我有

'视频-01,video_02 ......'

注:随机存储,名称不稳定,有时10个字符,有时候15个字符...因此不可能使用SUBSTRING

现在我遇到了麻烦,有些标题包含阿拉伯字符,我该如何更换全部字符,只是不停地数

+2

你或许应该使用一个“id”只是一个数字来识别视频,这似乎比你试图去做的更容易,更有效率。 –

+2

你是一个杀手! ('INDEX'的*,顺便说一下':D' *)为什么你不使用主键? –

+1

为什么你的问题是?你有什么尝试?你卡在哪里? – bidifx

回答

0

如果我理解正确的话你的问题,你可以使用REPLACE函数是这样的:

SELECT title 
FROM video 
WHERE REPLACE(REPLACE(title, '-', ' '), '_', ' ') < 'video 03'; 

这将返回“视频01”,“视频-02”,“video_02”等。

编辑:你也可以利用这一点,也就是分隔标题从数字部分的字母数字部分:

select 
    title, 
    first_digit, 
    left(title, first_digit-1) as alphanumeric_part, 
    mid(title, first_digit, length(title)-first_digit+1) as digits 
from (
    select 
    least(case when locate('0', title)>0 then locate('0', title) else length(title)+1 end, 
      case when locate('1', title)>0 then locate('1', title) else length(title)+1 end, 
      case when locate('2', title)>0 then locate('2', title) else length(title)+1 end, 
      case when locate('3', title)>0 then locate('3', title) else length(title)+1 end, 
      case when locate('4', title)>0 then locate('4', title) else length(title)+1 end, 
      case when locate('5', title)>0 then locate('5', title) else length(title)+1 end, 
      case when locate('6', title)>0 then locate('6', title) else length(title)+1 end, 
      case when locate('7', title)>0 then locate('7', title) else length(title)+1 end, 
      case when locate('8', title)>0 then locate('8', title) else length(title)+1 end, 
      case when locate('9', title)>0 then locate('9', title) else length(title)+1 end) first_digit, 
    title 
    from 
    video 
) video_pos 
where mid(title, first_digit, length(title)-first_digit+1)+0 < 2 

看到它here

+0

现在我遇到了麻烦,一些标题包含阿拉伯字符,我如何更换所有字符,并保持号码 – user2039770

+0

@ user2039770请看看我更新的答案,但我认为最好是规范化您的数据,并有两个不同的列标题之一,数字部分之一 – fthiella

1

或者,你可以做很多要求:

//string that needs to be customized 
$title = "video 03"; 
$title = str_replace("-", " ", $title); 

$title_array=explode(' ', $title); 
$number=$title_array[1]; 

echo $number; 
echo $title; 

做你的SQL这里由下视频号码检索所有视频