2016-11-04 30 views
0

我最近使此線程管理數據庫中的iPhone模型,以便能夠通過排除最後一個詞(這是容量)來選擇所有不同的模型, 。使用%LIKE%子句選擇並排除包含某些詞的結果

Select distinct first words of a field

  +-----------+--------------------------------+ 
      | id  |    model    | 
      +-----------+--------------------------------+ 
      |  1  |  Apple iPhone 4S 16Gb  | 
      |  2  |  Apple iPhone 4S 32Gb  | 
      |  3  |  Apple iPhone 4 8Gb  | 
      |  4  | Apple iPhone 6 Plus 32Gb | 
      |  5  | Apple iPhone 6 Plus 64Gb | 
      |  6  |  Apple iPhone 6 16Gb  | 
      +-----------+--------------------------------+ 

對於這個表,我有以下的輸出:
蘋果iPhone 4S
蘋果iPhone 4
蘋果iPhone 6加
蘋果iPhone 6

現在,我希望能夠爲每個模型獲得可用的容量領帶。 我想使用的LIKE %%條款,

SELECT * FROM `model` WHERE `value_model` LIKE '%Apple iPhone 6S%' 

這是工作,直到模型與「加號」出現了。 爲iPhone 4S的爲例我必須做到以下幾點:

SELECT * FROM `model` WHERE `value_model` LIKE '%Apple iPhone 4S%' 

我有這個輸入:
蘋果iPhone 4S 16GB的
蘋果iPhone 4S 32G的

但對於iPhone 6,它還將在我的結果中包含iPhone 6 Plus。

我怎麼能碰到這個,通過排除結果與'加',但也能夠不排除這個'加',當我要求一個'加'模型。

預先感謝您的:-)

+0

你能寫出輸出的完美解決方案您正在尋找 ? – Imanez

+0

只需獲得給定模型的結果。 對於iPhone 4S: 蘋果iPhone 4S 16GB,蘋果iPhone 4S 32G的 對於iPhone 6: 蘋果iPhone 6的16Gb – saperlipopette

回答

2

嗨這是你

SELECT * FROM `model` 
WHERE TRIM(REPLACE(NAME,SUBSTRING_INDEX(value_model,' ',-1),'')) LIKE '%Apple iPhone 6' 
+0

如果您認爲它是有用的,請將其標記爲正確的答案 –

+0

謝謝,它的作用像一個魅力! – saperlipopette

0

任何幫助,如果我理解正確:

SELECT * FROM `model` WHERE `value_model` LIKE '%Apple iPhone 6%' and `value_model` NOT LIKE '%Apple iPhone 6 Plus%' 
+0

這是我試過的查詢,但我得到這個輸出,即使有 「NOT LIKE」: http://img4.hostingpics.net/pics/754824output.png – saperlipopette

相關問題