Err1242 - 子查询返回多行

时间:2016-04-11 00:50:18

标签: mysql scripting subquery

这个脚本有什么问题? 我收到了错误

“[Err] 1242 - 子查询返回超过1行”。

如何解决这个问题?谢谢!

    SELECT
  j.id AS JobID,
        CASE WHEN j.id = (SELECT o.attachment_id from origins o where o.attachment_type="Job" AND o.alliance_partner_id = 12) then "Channel 1"
        WHEN j.id = (SELECT o.attachment_id from origins o where o.attachment_type="Job" AND o.alliance_partner_id = 15) then "Channel 2"
        WHEN j.id = (SELECT t.record_id FROM tracked_records t where t.record_type = 'Job') then "Channel 3"
        WHEN j.id = (SELECT r.job_id from job_referrers r where r.url IN ('ios','android')) then "Channel 4"
        END AS "Channels"
FROM jobs j
GROUP BY j.id
;

1 个答案:

答案 0 :(得分:0)

语法修复查询的一个选项是对每个子WHERE IN使用SELECT

SELECT j.id AS JobID,
    CASE WHEN j.id IN (SELECT o.attachment_id from origins o where o.attachment_type="Job" AND o.alliance_partner_id = 12) THEN "Channel 1"
         WHEN j.id IN (SELECT o.attachment_id from origins o where o.attachment_type="Job" AND o.alliance_partner_id = 15) THEN "Channel 2"
         WHEN j.id IN (SELECT t.record_id FROM tracked_records t where t.record_type = 'Job') THEN "Channel 3"
         WHEN j.id IN (SELECT r.job_id from job_referrers r where r.url IN ('ios','android')) THEN "Channel 4"
    END AS "Channels"
FROM jobs j
GROUP BY j.id

请注意,虽然这可能会使查询运行,但逻辑上可能不是您最初的意图。所以你应该重新审视你在这里想要实现的目标。