SQL查询针对多个AND条件返回0行

时间:2018-07-11 12:40:13

标签: mysql sql database phpmyadmin

我想通过使用过滤器对以下详细信息进行AND / OR运算,基于最后3列对这2行进行分类: enter image description here

现在我正在使用的表结构需要多个JOIN条件:

1.m_recipient enter image description here 样本数据(m_recipient): enter image description here

2.m_invites_recipient enter image description here 样本数据(m_invites_recipient):

enter image description here

3.m_custom_field_values enter image description here 样本数据(m_custom_field_values): enter image description here

例如,现在当我尝试获取值为rinnegan = yes且sharedan = no的用户时,结果为空行(答案应为第一行。而如果我尝试使用rinnegan = yes或sharedan = no,则我两行都收。

SQL查询:-

SELECT DISTINCT m_recipient.id, m_recipient.sd_recipient_id, 
  m_recipient.campaign_id, m_recipient.user_id, m_recipient.userid, 
  m_recipient.first_name, m_recipient.last_name, m_recipient.email, 
  m_recipient.survey_link, m_recipient.unsubscribe 
FROM m_invites_recipient 
JOIN m_recipient ON m_recipient.id = m_invites_recipient.m_recipient_sd_id 
LEFT OUTER JOIN m_custom_field_values ON m_custom_field_values.related_to_id = m_recipient.id 
WHERE m_invites_recipient.m_invites_id = 6 
  AND m_recipient.unsubscribe = 0  
  AND ( m_custom_field_values.custom_field_name = "rinnegan" 
    AND m_custom_field_values.value = "yes" 
    AND m_invites_recipient.m_invites_id = 6 
    AND m_recipient.unsubscribe = 0 )  
  AND ( m_custom_field_values.custom_field_name = "sharingan" 
    AND m_custom_field_values.value = "no" 
    AND m_invites_recipient.m_invites_id = 6 
    AND m_recipient.unsubscribe = 0 ) 

4 个答案:

答案 0 :(得分:1)

也许您想匹配的行要么与所有第一组带括号的条件匹配,要么与所有第二组带括号的条件匹配?

如果是这样,您可以简单地执行以下操作:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>A Row</td>
    <td><input name="Details[0].Amount" /></td>
  </tr>
</table>
<button>Clone</button>

答案 1 :(得分:0)

查询的问题是WHERE条件下的逻辑。可以将其重写为以下内容:

WHERE m_invites_recipient.m_invites_id = 6 
  AND m_recipient.unsubscribe = 0  
  AND ( m_custom_field_values.custom_field_name = "rinnegan" 
    AND m_custom_field_values.value = "yes")  
  AND ( m_custom_field_values.custom_field_name = "sharingan" 
    AND m_custom_field_values.value = "no")

显然不会返回任何行,因为没有行都等于“ rinnegan” AND “ sharingan”。

编辑

这里的问题是,您试图在where条件中使用多行数据,这可能会很棘手。可能有一种更优雅的方法来执行此操作,但是可能的解决方案是使用子查询来找到问题的“答案”。类似于以下内容:

WITH 
rinneganQuery AS (
    SELECT value AS rinneganAnswer, related_to_id FROM m_custom_field_values 
    WHERE custom_field_name = 'rinnegan'
),
sharinganQuery AS (
    SELECT value AS sharinganAnswer, related_to_id FROM m_custom_field_values 
    WHERE custom_field_name = 'sharingan'
)

SELECT DISTINCT inv.id, inv.sd_recipient_id, 
  inv.campaign_id, inv.user_id, inv.userid, 
  inv.first_name, inv.last_name, inv.email, 
  inv.survey_link, inv.unsubscribe 
FROM m_invites_recipient inv
INNER JOIN m_recipient rec ON inv.m_recipient_sd_id = rec.id 
INNER JOIN rinneganQuery rq ON inv.m_recipient_sd_id = rq.related_to_id
INNER JOIN sharinganAnswer sq ON inv.m_recipient_sd_id = sq.related_to_id
WHERE inv.m_invites_id = 6 
  AND rec.unsubscribe = 0
  AND rq.rinneganAnswer = 'yes'
  AND sq.sharinganAnswer = 'no';

答案 2 :(得分:0)

只是为了好玩,这是一个有效查询的示例:

o String param 

答案 3 :(得分:0)

正如@ADyson所说,您可以使用多个联接。

SELECT DISTINCT m_recipient.id, 
m_recipient.sd_recipient_id, 
m_recipient.campaign_id, 
m_recipient.user_id, 
m_recipient.userid, 
m_recipient.first_name, 
m_recipient.last_name, 
m_recipient.email, 
m_recipient.survey_link, 
m_recipient.unsubscribe 
FROM   m_invites_recipient 
   JOIN m_recipient 
     ON m_recipient.id = m_invites_recipient.m_recipient_sd_id 
   JOIN m_custom_field_values AS a0 
     ON a0.related_to_id = m_recipient.id 
   JOIN m_custom_field_values AS a1 
     ON a1.related_to_id = m_recipient.id 
WHERE  m_invites_recipient.last_opened = 0 
   AND m_invites_recipient.last_clicked = 0 
   AND m_invites_recipient.m_invites_id = 6 
    OR ( a0.custom_field_name = "sharingan" 
         AND a0.value = "no" 
         AND m_invites_recipient.m_invites_id = 6 
         AND m_recipient.unsubscribe = 0 ) 
    OR ( a1.custom_field_name = "rinnegan" 
         AND a1.value = "yes" 
         AND m_invites_recipient.m_invites_id = 6 
         AND m_recipient.unsubscribe = 0 )

例如,要比较两个以上的行,您只需要再添加一个JOIN