2016-01-13 55 views
0

我拉我的頭髮在這裏試圖讓這個工作。MYSQL像,或者和,給出所有結果

這是我的代碼:

 SELECT a1.* 
     , t2.`name` AS project_name 
     , t2.`id` AS project_id 
     , u2.`fname` AS fname 
    , u2.`lname` AS lname 
    , u2.`color` AS color 
    , u2.`id` AS user_id 
    , u3.`fname` AS creatorfname 
    , u3.`lname` AS creatorlname 

    FROM `activities` AS a1 
    LEFT JOIN `projects` AS t2 ON a1.`projectid`=t2.`id` 
    LEFT JOIN `users` AS u2 ON a1.`userid`=u2.`id` 
    LEFT JOIN `users` AS u3 ON a1.`creatorid`=u3.`id` 
    LEFT JOIN `organisations` AS o1 ON u2.`orgid`=o1.`id` WHERE 
      o1.`id`=:orgid AND a1.`active`=1 OR a1.`postarea` LIKE '%newyork%' 
      OR a1.`streetname` LIKE '%newyork%' 
      OR a1.`companyname` LIKE '%newyork%' 
      OR a1.`postarea` LIKE '%newyork%' 
      OR a1.`orgnumber` LIKE '%newyork%' 
      OR a1.`postcode` LIKE '%newyork%' 
      OR a1.`homepage` LIKE '%newyork%' 
      OR a1.`phone1` LIKE '%newyork%' 
      OR a1.`phone2` LIKE '%newyork%' 
      OR a1.`phoneswitch` LIKE '%newyork%' 
      OR a1.`status` LIKE '%newyork%' 
      OR a1.`turnovermin` LIKE '%newyork%' 
      OR a1.`turnovermax` LIKE '%newyork%' 
      OR a1.`description` LIKE '%newyork%' 
      OR a1.`comment` LIKE '%newyork%' 
      OR a1.`email` LIKE '%newyork%' AND a1.`projectid`=:projectid  
      AND a1.`userid`=:userid AND a1.`date`>=:datefrom AND 
      a1.`date`<=:dateto ORDER BY a1.`date` DESC 

的問題是,我得到一個基於或類似語句的每個結果。 因此我使用PDO類,例如:orgid。它應該考慮所有的陳述和標準。我嘗試過()和...等,但沒有任何結果。

希望得到一些幫助。

+1

'WHERE O1 \'ID \'=:。ORGID AND(OR值一束)和A 1 \'專案編號\'=:專案編號和A 1 \'的userid \'=:用戶標識和A1 \。 'date \'> =:datefrom AND a1 \'date \'<=:dateto' – MonkeyZeus

+0

您需要了解邏輯括號。 – Utkanos

+0

你必須告訴我們你想要的結果。你的陳述對我來說很奇怪,太複雜了。你爲什麼要問「紐約」10次? – etalon11

回答

1

使用()各地OR S,AND運營商具有較高的優先級因而OR之前應用。

SELECT a1.* 
    , t2.`name` AS project_name 
    , t2.`id` AS project_id 
    , u2.`fname` AS fname 
, u2.`lname` AS lname 
, u2.`color` AS color 
, u2.`id` AS user_id 
, u3.`fname` AS creatorfname 
, u3.`lname` AS creatorlname 

FROM `activities` AS a1 
LEFT JOIN `projects` AS t2 ON a1.`projectid`=t2.`id` 
LEFT JOIN `users` AS u2 ON a1.`userid`=u2.`id` 
LEFT JOIN `users` AS u3 ON a1.`creatorid`=u3.`id` 
LEFT JOIN `organisations` AS o1 ON u2.`orgid`=o1.`id` 
WHERE 
     o1.`id`=:orgid AND a1.`active`=1 AND 
     ( a1.`postarea` LIKE '%newyork%' 
     OR a1.`streetname` LIKE '%newyork%' 
     OR a1.`companyname` LIKE '%newyork%' 
     OR a1.`postarea` LIKE '%newyork%' 
     OR a1.`orgnumber` LIKE '%newyork%' 
     OR a1.`postcode` LIKE '%newyork%' 
     OR a1.`homepage` LIKE '%newyork%' 
     OR a1.`phone1` LIKE '%newyork%' 
     OR a1.`phone2` LIKE '%newyork%' 
     OR a1.`phoneswitch` LIKE '%newyork%' 
     OR a1.`status` LIKE '%newyork%' 
     OR a1.`turnovermin` LIKE '%newyork%' 
     OR a1.`turnovermax` LIKE '%newyork%' 
     OR a1.`description` LIKE '%newyork%' 
     OR a1.`comment` LIKE '%newyork%' 
     OR a1.`email` LIKE '%newyork%' 
     ) 
     AND a1.`projectid`=:projectid  
     AND a1.`userid`=:userid AND a1.`date`>=:datefrom AND 
     a1.`date`<=:dateto ORDER BY a1.`date` DESC 
+0

非常感謝你!你真的救了我的夜晚:D那個人完美無缺。我會用圓括號來看看這些東西。 – user1158040

2

您需要了解邏輯括號。

a AND b OR c OR d意味着邏輯污染。括號告訴處理器能夠執行哪些操作了第一,而不是排名每個相等:

a AND (b OR c) - 如果A是真實的和B或C也真

(a AND b) OR c - 如果A和B都爲真,或不管他們的,如果C是真實的,而不是

+1

說的很好@Utkanos! –