JOINS SQL中的OR操作(MYSQL)

时间:2015-05-06 13:40:15

标签: mysql sql join

使用MYSQL

SELECT m.proposalId, m.title, n.stageNumber, n.committeeId, n.modifiedDate, o.msNumber , o.description,o.ics,o.edition FROM mystands_Proposal m 
INNER join  
    mystands_ProjectLifecycle n on m.proposalId = n.proposalId 
INNER join  
    mystands_Project o on m.proposalId = o.proposalId 
WHERE 
    n.newState=0  AND n.committeeId=79827 AND (n.assignedTo=29913 OR n.actionBy=29913) AND n.proposalId LIKE '%sdas%' AND o.projectNumber LIKE '%sdass%' AND n.stageNumber=40.92 AND o.category=1 AND o.degreeofCorrespondence=1 AND o.msNumber LIKE '%sdas%' AND (n.modifiedDate <='2015-05-15' AND n.stageNumber=40.2) AND (n.modifiedDate <='2015-05-07' AND n.stageNumber=30.99) AND (n.modifiedDate <='2015-05-27' AND n.stageNumber=55.99)

我为搜索功能的三个表做内部联接,在java中,如果用户输入值,则将值附加到sql查询中。上面的代码适用于字段之间的AND操作。

我如何表演&#34;或&#34;用户输入的字段和显示结果的操作 实际上如果我尝试过这个

SELECT m.proposalId, m.title, n.stageNumber, n.committeeId, n.modifiedDate, o.msNumber , o.description,o.ics,o.edition FROM mystands_Proposal m 
INNER join  
    mystands_ProjectLifecycle n on m.proposalId = n.proposalId 
INNER join
    mystands_Project o on m.proposalId = o.proposalId  
WHERE 
    n.newState=0      OR n.committeeId=80246 OR (n.assignedTo=79977 OR n.actionBy=79977) OR n.proposalId LIKE '%ads%' OR o.projectNumber LIKE '%sds%' OR n.stageNumber=30.99 OR o.category=1 OR o.degreeofCorrespondence=1 OR o.msNumber LIKE '%sadsa%' OR (n.modifiedDate <='2015-05-22' AND n.stageNumber=40.2) OR (n.modifiedDate <='2015-05-22' AND n.stageNumber=30.99) OR (n.modifiedDate <='2015-05-29' AND n.stageNumber=55.99) 

现在发生的事情是它给了我内部连接的结果,因为newState = 0是真的并且整个条件变为true并且在三个表上得到内部连接的结果并且结果没有按需要过滤

你能帮忙出错吗? 感谢

3 个答案:

答案 0 :(得分:0)

如果你得到了所有东西,因为newstate = 0总是如此,那么你只需要把它,如果我误解,那么我道歉但这似乎是我理解的问题:

  

“因为newState = 0为真,整个条件为真”

n.newState=0 AND (n.committeeId=80246 OR (n.assignedTo=79977 OR n.actionBy=79977) OR n.proposalId LIKE '%ads%' OR o.projectNumber LIKE '%sds%' OR n.stageNumber=30.99 OR o.category=1 OR o.degreeofCorrespondence=1 OR o.msNumber LIKE '%sadsa%' OR (n.modifiedDate <='2015-05-22' AND n.stageNumber=40.2) OR (n.modifiedDate <='2015-05-22' AND n.stageNumber=30.99) OR (n.modifiedDate <='2015-05-29' AND n.stageNumber=55.99))

在您的代码中的某处记录您添加了多少条件,如果大于0,请添加'AND(......),因为我不知道生成此查询的代码是什么看起来我无法再帮你了

答案 1 :(得分:0)

WHERE 
n.newState=0
    AND (
    n.committeeId=80246 OR (n.assignedTo=79977 OR n.actionBy=79977) OR n.proposalId LIKE '%ads%' OR o.projectNumber LIKE '%sds%' OR n.stageNumber=30.99 OR o.category=1 OR o.degreeofCorrespondence=1 OR o.msNumber LIKE '%sadsa%' OR (n.modifiedDate <='2015-05-22' AND n.stageNumber=40.2) OR (n.modifiedDate <='2015-05-22' AND n.stageNumber=30.99) OR (n.modifiedDate <='2015-05-29' AND n.stageNumber=55.99)
    )

答案 2 :(得分:0)

目前尚不清楚你的目标是什么。如果你能为sqlfiddle提供数据样本和预期结果,那将会有很大的帮助。

一般而言,从我的经验来看,通常你应该有:

SELECT 
  m.proposalId, m.title, 
  n.stageNumber, n.committeeId, n.modifiedDate, 
  o.msNumber , o.description,o.ics,o.edition 
FROM mystands_Proposal m 
INNER join  
    mystands_ProjectLifecycle n 
ON m.proposalId = n.proposalId 
   AND n.newState=0
   AND ( n.committeeId=80246 
        OR n.assignedTo=79977 
        OR n.actionBy=79977
        OR n.proposalId LIKE '%ads%'
        OR n.stageNumber=30.99
        OR (n.modifiedDate <='2015-05-22' AND n.stageNumber=40.2) 
        OR (n.modifiedDate <='2015-05-22' AND n.stageNumber=30.99) 
        OR (n.modifiedDate <='2015-05-29' AND n.stageNumber=55.99)
   )
INNER join
    mystands_Project o 
ON m.proposalId = o.proposalId  
   AND ( o.projectNumber LIKE '%sds%' 
        OR o.category=1
        OR o.degreeofCorrespondence=1 
        OR o.msNumber LIKE '%sadsa%')