注册 登录
编程论坛 SQL Server论坛

用in做的子查询返回1200行的时候前面主句就会报错子查询不止一个,但返回1050行的时候是正常的。

su13800 发布于 2017-08-16 10:11, 2053 次点击
SELECT
    tb_publicvoice.*, (
        SELECT
            did
        FROM
            tb_daily_pv
        WHERE
            tb_daily_pv.pvid = tb_publicvoice.id

    ) AS daily_id,
    (
        SELECT
            (
                SELECT
                    CAST (dispose_doc_no AS VARCHAR) + ','
                FROM
                    tb_pv_dispose
                WHERE
                    tb_pv_dispose.id

 = tb_publicvoice.id

 FOR xml path ('')
            )
    ) AS dispose_doc_no,
    (
        SELECT
            daily_id
        FROM
            tb_pv_feedback_daily
        WHERE
            tb_pv_feedback_daily.id

 = tb_publicvoice.id

    ) AS feedback_daily_id,
    (
        SELECT
            COUNT (*)
        FROM
            tb_pv_comment
        WHERE
            tb_pv_comment.id

 = tb_publicvoice.id

    ) AS comment_count,
    (
        SELECT
            content
        FROM
            tb_pv_feedback
        WHERE
            tb_pv_feedback.id

 = tb_publicvoice.id

        AND tb_pv_feedback.type = 0
    ) AS docFeedback,
    (
        SELECT
            content
        FROM
            tb_pv_feedback
        WHERE
            tb_pv_feedback.id

 = tb_publicvoice.id

        AND tb_pv_feedback.type = 1
    ) AS webFeedback
FROM
    tb_publicvoice
WHERE
    tb_publicvoice.id

 IN (
        SELECT
            pvid
        FROM
            tb_pv_notify
        WHERE
            tb_pv_notify.uid = 'admin'
    )
AND tb_publicvoice.createtime < '2017-08-16'
AND tb_publicvoice.createtime > '2016-08-16'
ORDER BY
    tb_publicvoice.createtime DESC
4 回复
#2
su138002017-08-16 10:13
大神在哪里出来指教下。
#3
mywisdom882017-08-16 14:42
看了下,好像没什么问题.
SELECT
 tb_publicvoice.*,
 ( SELECT did FROM tb_daily_pv WHERE tb_daily_pv.pvid = tb_publicvoice.id ) AS daily_id,
 ( SELECT ( SELECT CAST (dispose_doc_no AS VARCHAR) + ',' FROM tb_pv_dispose WHERE tb_pv_dispose.id = tb_publicvoice.id  FOR xml path ('')) ) AS dispose_doc_no,
 ( SELECT daily_id FROM  tb_pv_feedback_daily WHERE tb_pv_feedback_daily.id  = tb_publicvoice.id ) AS feedback_daily_id,
 ( SELECT COUNT (*) FROM tb_pv_comment WHERE  tb_pv_comment.id = tb_publicvoice.id ) AS comment_count,
 ( SELECT content FROM tb_pv_feedback WHERE tb_pv_feedback.id = tb_publicvoice.id AND tb_pv_feedback.type = 0) AS docFeedback,
 ( SELECT content FROM tb_pv_feedback WHERE tb_pv_feedback.id = tb_publicvoice.id AND tb_pv_feedback.type = 1) AS webFeedback

 FROM tb_publicvoice
 WHERE
  tb_publicvoice.id  IN ( SELECT pvid FROM tb_pv_notify WHERE tb_pv_notify.uid = 'admin' )
  AND tb_publicvoice.createtime < '2017-08-16'
  AND tb_publicvoice.createtime > '2016-08-16'
 ORDER BY tb_publicvoice.createtime DESC

#4
mywisdom882017-08-16 14:47
有可能出现问题的,可以在这
( SELECT ( SELECT CAST (dispose_doc_no AS VARCHAR) + ',' FROM tb_pv_dispose WHERE tb_pv_dispose.id = tb_publicvoice.id  FOR xml path ('')) ) AS dispose_doc_no,
你把这个先去掉看看.
#5
mingfei2017-08-21 17:20
我感觉 可能是有数据问题
1