我有這個MySQL查詢。MySQL喜歡多個值
我有這個內容
sports,shopping,pool,pc,games
shopping,pool,pc,games
sports,pub,swimming, pool, pc, games
爲什麼會出現這種類似的查詢不起作用數據庫字段? 我需要與運動或酒吧或兩個領域?
SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')
我有這個MySQL查詢。MySQL喜歡多個值
我有這個內容
sports,shopping,pool,pc,games
shopping,pool,pc,games
sports,pub,swimming, pool, pc, games
爲什麼會出現這種類似的查詢不起作用數據庫字段? 我需要與運動或酒吧或兩個領域?
SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')
該(a,b,c)
列表僅適用於in
。對於like
,你必須使用or
:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
您的查詢應該是SELECT * FROM `table` WHERE find_in_set(interests, "sports,pub")>0
我的理解是,你存儲在你的表,這是一個誤解的一個領域的利益。你應該明確地有一個「興趣」表。這樣做的
比較快的方式:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
是這樣的:
WHERE interests REGEXP 'sports|pub'
發現這個解決方案在這裏:http://forums.mysql.com/read.php?10,392332,392950#msg-392950
更多REGEXP這裏:http://www.tutorialspoint.com/mysql/mysql-regexps.htm
唐「T忘了,如果你的AND
參數
篩選後,使用此功能使用括號:
WHERE id=123 and(interests LIKE '%sports%' OR interests LIKE '%pub%')
像@Alexis Dufrenoy建議,查詢可能在manualSELECT * FROM `table` WHERE find_in_set('sports', interests)>0 OR find_in_set('pub', interests)>0
更多信息。
我認爲它應該是'SELECT * FROM table WHERE find_in_set(interest,'sports,pub')',但是這種技術在大多數情況下可能會勝過正則表達式。 – 2017-09-14 04:02:06
你是對的。我編輯了我的答案。 – 2018-01-23 09:54:42