簡體   English   中英

SQL查詢未通過NOT LIKE返回期望的結果

[英]SQL Query not returning desired result with NOT LIKE

我正在運行一個簡單的查詢,其中使用NOT LIKE比較TEXT列,但結果不正確。 嘗試了很多但沒有運氣。 這是查詢:

SELECT *
FROM `restaurant_session_log`
WHERE `restId` = '176'
OR branchId = '203'
OR  `multi_vendorId` LIKE '%,176%' 
OR  `multi_vendorId` LIKE '%,176,%' 
OR  `multi_vendorId` LIKE '%176,%' 
OR  `multi_vendorId` LIKE '%[176]%' 
AND (`excluded_branch_id` NOT LIKE '%,203%'  OR `excluded_branch_id` NOT LIKE '%,203,%'  OR `excluded_branch_id` NOT LIKE '%203,%'  OR `excluded_branch_id` NOT LIKE '%[203]%' )

結果如下: 屏幕截圖

現在正確的結果將僅包含id = 27707第二行,因為我在查詢中提到要在其中excluded_branch_id != %203%結果帶入結果,但我不明白為什么要在excluded_branch_id列中給出包含203的行。

請幫忙!

不要在單個字符串中存儲多個值! 不要將數字值放在字符串中! 這是問題的根本原因。

有時,您無法更改某人的非常非常糟糕的數據建模決策。 您明顯的問題是括號。

但是,如果您使用find_in_set() ,則邏輯將得到簡化。 我認為您打算:

WHERE (`restId` = '176' OR branchId = '203' OR
       find_in_set(176, `multi_vendorId`)
      ) AND
      (find_in_set(203, `excluded_branch_id`)

您可以使用REGEXP簡化匹配。 如果它的兩邊都有單詞邊界,則以下內容與203相匹配:

(excluded_branch_id IS NULL OR excluded_branch_id NOT REGEXP '[[:<:]]203[[:>:]]')

例如:

SELECT
    '203,111' REGEXP '[[:<:]]203[[:>:]]', -- 1
    '111,203' REGEXP '[[:<:]]203[[:>:]]', -- 1
    '1,203,1' REGEXP '[[:<:]]203[[:>:]]', -- 1
    '1120311' REGEXP '[[:<:]]203[[:>:]]'  -- 0

$ query =“選擇*從電影中,year_released不像'200_';”

$ result = db_query($ query,array(':movies'=> $ value));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM