This is my query
這是我的疑問
SELECT t.*
FROM `User` `t`
INNER JOIN (
SELECT cv.relatedId
FROM CustomValue cv
WHERE (cv.customFieldId=11 AND (cv.fieldValue like '%dsasda%')) OR (cv.customFieldId=15 AND (cv.fieldValue like '%1%'))
GROUP BY cv.relatedId
HAVING count(*) >= 1
) tblcv ON tblcv.relatedId = t.id
I need to do add a AND CONDITION. i am wondering how to.
我需要添加一個AND CONDITION。我想知道如何。
This is the data set.
這是數據集。
As circled in blue, I need to compare against both records for a particular relatedId
.
如藍色圓圈所示,我需要比較特定relatedId的兩個記錄。
For example, for related id 4031 I need to check if customFieldId
= 11 and fieldValue
like %dsasda%
and if customFieldId
= 15 and fieldValue
like '%1%'
例如,對於相關的id 4031,我需要檢查customFieldId = 11和fieldValue是否為%dsasda%,如果customFieldId = 15,fieldValue是否為'%1%'
So the result of query for this data set should have 4031
relatedId
as an answer but I get all 3 related id result. it seems to be using OR CONDITION.
因此查詢此數據集的結果應該有4031 relatedId作為答案,但我得到所有3個相關的id結果。它好像在使用OR CONDITION。
When try to use AND condition. i dont get any results. This is with AND condition.
當嘗試使用AND條件時。我沒有得到任何結果。這是AND條件。
SELECT cv.relatedId
FROM CustomValue cv
WHERE (cv.customFieldId=11 AND (cv.fieldValue like '%dsasda%')) AND (cv.customFieldId=15 AND (cv.fieldValue like '%1%'))
GROUP BY cv.relatedId
HAVING count(*) >= 1
1
If you want to match both conditions then you need to match at least 2 rows in your having
如果您想要匹配這兩個條件,那么您需要匹配至少2行
HAVING count(*) >= 1
^ remove equality operator
You're getting back the 2 other id's because they each match 1 row (you've circled them).
你得到了另外兩個id,因為它們各自匹配1行(你已經圈出了它們)。
If (relatedId,fieldValue)
is not unique, then use distinct
to make sure each group has both values.
如果(relatedId,fieldValue)不唯一,則使用distinct來確保每個組都具有這兩個值。
HAVING count(distinct fieldValue) > 1
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/11/13/7200948d422d6bdf1a486c29bc8909d1.html。