2013-02-04 79 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這裏由下視頻號碼檢索所有視頻