2013-04-29 81 views
1

我正在尋找一種方法來根據另一列的數據在一列中選擇某些內容。我有2個表格:MYSQL - 基於動態值選擇

input_table - has a column called "year_of_birth" 
result_table - has a column called "notes" 

你看,那個「註釋」列可能包含一年,像「1980」。我需要能夠選擇筆記字段包含與year_of_birth字段匹配的行。我有我的發言寫成這樣至今:

select inp.year_of_birth, res.notes, res.result 
from order_input inp join order_result res on inp.order_id = res.order_id 
where res.result !='no match' 
and res.notes like '%' + inp.year_of_birth + '%' 

現在我越來越thta說:「將varchar值‘%’轉換爲數據類型爲int時轉換失敗的錯誤,我不知道我在做錯誤的,因爲我有我使用了這種類型的它的作品串了類似的聲明......

回答

2

您需要使用CONCAT代替+

res.notes like CONCAT('%', inp.year_of_birth, '%') 
+0

是'''會嘗試將'%'添加到'inp.year_of_birth',在嘗試將'%'轉換爲整數的過程中。這給你你的錯誤。 – 2013-04-29 21:44:21

0

試試這個:

select inp.year_of_birth, res.notes, res.result from order_input as inp join order_resultas res on inp.order_id = res.order_id where res.result <> 'no match' and res.notes like CONCAT('%', inp.year_of_birth, '%')