不会执行的SQL查询

时间:2011-01-26 01:35:35

标签: sql mysql

我是SQL的新手,只是为什么这段代码不能执行而感到茫然。我知道错误,但没有结果。无论我等多久,代码都会继续处理。我知道这个问题可能与我的特定数据库有关,但我想知道是否有明显的错误。

此外,如果它与我在我的数据库中进行的调用有关,我将如何进行调试,因为我没有正式错误?

SELECT cr.COMMUNICATIONS_ID AS ANSWER_ID, cr.CONSUMER_ID as VIEWER_ID, 
       cr.ACTION_LOG_ID, nc.PARENT_COMMUNICATIONS_ID AS QUESTION_ID,
       cirm.CONSUMER_RATING AS VIEWER_INTEREST, 
       cim.CONSUMER_INTEREST_EXPERT_ID AS DOMAIN
FROM (
       SELECT 234 AS CONSUMER_ID, ACTION_LOG_ID, COMMUNICATIONS_ID 
        FROM consumer_action_log 
        WHERE COMM_TYPE_ID=4
      ) AS cr
    JOIN network_communications AS nc ON 
        cr.COMMUNICATIONS_ID=nc.COMMUNICATIONS_ID
    JOIN communication_interest_mapping AS cim ON 
       nc.PARENT_COMMUNICATIONS_ID=cim.COMMUNICATION_ID
    JOIN consumer_interest_rating_mapping AS cirm ON 
       cr.CONSUMER_ID=cirm.CONSUMER_ID AND           
       cim.CONSUMER_INTEREST_EXPERT_ID=cirm.CONSUMER_INTEREST_ID;

2 个答案:

答案 0 :(得分:2)

我没有看到任何明显的(语法)错误。您可能在联接中出现了逻辑错误。要进行调试,请删除连接,直到获得结果为止。然后开始添加连接。这应该会显示你出错的地方。

答案 1 :(得分:2)

子查询中的派生列可能会抛弃MySQL。请尝试此查询以获得相同的结果

SELECT cr.COMMUNICATIONS_ID AS ANSWER_ID, cirm.CONSUMER_ID as VIEWER_ID, 
       cr.ACTION_LOG_ID, nc.PARENT_COMMUNICATIONS_ID AS QUESTION_ID,
       cirm.CONSUMER_RATING AS VIEWER_INTEREST, 
       cim.CONSUMER_INTEREST_EXPERT_ID AS DOMAIN
FROM (
       SELECT ACTION_LOG_ID, COMMUNICATIONS_ID 
        FROM consumer_action_log 
        WHERE COMM_TYPE_ID=4
      ) AS cr
    JOIN network_communications AS nc ON 
        cr.COMMUNICATIONS_ID=nc.COMMUNICATIONS_ID
    JOIN communication_interest_mapping AS cim ON 
       nc.PARENT_COMMUNICATIONS_ID=cim.COMMUNICATION_ID
    JOIN consumer_interest_rating_mapping AS cirm ON 
       cim.CONSUMER_INTEREST_EXPERT_ID=cirm.CONSUMER_INTEREST_ID
WHERE cirm.CONSUMER_ID = 234