久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在子查詢的子查詢中使用父查詢的列

Use column of parent query in subquery of a subquery(在子查詢的子查詢中使用父查詢的列)
本文介紹了在子查詢的子查詢中使用父查詢的列的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

使用此查詢:

SELECT
    `id`,
    `type`,
    `subtype`,
    `title`,
    `shortdesc`,
    (SELECT COUNT(*)
     FROM `story_comments`
     WHERE `parent_id` = t1.`id`) as comments,
    (SELECT
        (ROUND( (
            SELECT (SUM(`rating` * `count`) / SUM(`count`) ) ) * 2) ) / 2 as result
            FROM
                    (SELECT rating, COUNT(*) as count
                     FROM `story_ratings` WHERE `parent_id` = t1.`id`
                     GROUP BY rating) as val) as rating,
    `calls`,
    `user`
FROM
    `storys` t1
WHERE
    `open` = 1 AND
    `modremove` = 0 AND
    `modblock` = ''
ORDER BY
    `opening`
DESC LIMIT 16;

我收到此錯誤:#1054 - 'where 子句' 中的未知列 't1.id',這是由子查詢中的子查詢(FROM 之后的子查詢)引起的.

I get this error: #1054 - Unknown column 't1.id' in 'where clause', which is caused by the subquery in the subquery (subquery after FROM).

但是第一個子查詢中的 t1.id 工作正常.為什么我不能在 FROM 子查詢中使用它?我也試過變量,但也沒有用:

But the t1.id in the first subquery is working fine. Why cant I use it in the FROM-subquery? I also tried variables, which also didnt work:

SELECT @i := `id` id, `type`, `subtype`, `title`, `shortdesc`, (SELECT COUNT(*) FROM `story_comments` WHERE `parent_id` = t1.`id`) as comments, 

(SELECT (ROUND( (SELECT (SUM(`rating` * `count`) / SUM(`count`) ) ) * 2) ) / 2 as result FROM (SELECT rating, COUNT(*) as count FROM `story_ratings` WHERE `parent_id` = @i GROUP BY rating) as val) as rating,

`calls`, `user` FROM `storys` t1 WHERE `open` = 1 AND `modremove` = 0 AND `modblock` = '' ORDER BY `opening` DESC LIMIT 16;

使用 @i 變量,結(jié)果在每一行都返回 NULL,這是怎么回事.

With the @i variable, result returned NULL on every row, what is wrong.

推薦答案

哇.這么多嵌套的子查詢.不要將查詢嵌套到地球的盡頭,而是使用 JOIN 并聚合您的數(shù)據(jù)以計算您需要的內(nèi)容.我不得不對你的表結(jié)構(gòu)進(jìn)行一些猜測,因為你沒有提供它們(在發(fā)布數(shù)據(jù)庫問題時你應(yīng)該總是這樣做).

Wow. So many nested subqueries. Instead of nesting queries to the ends of the earth, use JOINs and aggregate your data to calculate what you need. I had to make some guesses about your table structures because you didn't supply them (something that you should always do when posting a database question).

SELECT
    S.id,
    S.type,
    S.subtype,
    S.title,
    S.shortdesc,
    COUNT(DISTINCT SC.id) AS comments,
    AVG(SR.rating) AS rating,
    calls,
    user
FROM
    Storys S  -- Storys isn't the plural of Story, BTW
LEFT OUTER JOIN Story_Comments SC ON SC.parent_id = S.id
LEFT OUTER JOIN Story_Ratings SR ON SR.parent_id = S.id
WHERE
    S.open = 1 AND
    S.modremove = 0 AND
    S.modblock = ''
GROUP BY
    S.id,
    S.type,
    S.subtype,
    S.title,
    S.shortdesc,
    S.calls,
    S.user
ORDER BY
    opening
DESC LIMIT 16;

我不認(rèn)為* 2 .../2"會根據(jù)各種括號執(zhí)行您認(rèn)為的操作,并且根據(jù)您的評級的數(shù)據(jù)類型,此處的舍入可能不正確 列(同樣,沒有表結(jié)構(gòu),所以我一直在猜測).

I don't think that the "* 2 ... / 2" does what you think it does, based on the various parentheses and the rounding might not be right here depending on the data type of your rating column (again, no table structures, so I'm stuck guessing).

我手邊沒有 MySQL 服務(wù)器,但在 SQL Server 上測試此代碼(針對 ROUND 函數(shù)中的差異進(jìn)行調(diào)整)給出了與您的第一次查詢相同的結(jié)果.

I don't have a MySQL server handy, but testing this code on SQL Server (adjusting for difference in the ROUND function) gave the same results as your first query.

這篇關(guān)于在子查詢的子查詢中使用父查詢的列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應(yīng)該使用什么 SQL Server 數(shù)據(jù)類型來存儲字節(jié) [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數(shù)據(jù))
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉(zhuǎn)換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應(yīng)該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 久久99精品久久久久久青青日本 | 欧美日韩高清在线观看 | 欧美三级电影在线播放 | 国产精品福利网 | 18gay男同69亚洲网站 | av国产精品 | 一级黄色毛片 | 午夜欧美一区二区三区在线播放 | 成人夜晚看av | 欧美中文一区 | av中文字幕网站 | 视频一区在线观看 | 中文字幕国产在线 | 国产精品福利在线 | 色综合久久久久 | 蜜臀久久 | 日韩av一区二区在线观看 | h视频在线看 | 一区二区国产精品 | 日本欧美国产在线 | 日日操夜夜操天天操 | 91视频久久久久 | 国产精品久久久久久久久久久久冷 | 欧美精品在线一区二区三区 | 国产成人99久久亚洲综合精品 | 天天操天天摸天天爽 | 伊人国产精品 | 精品三级在线观看 | 国产视频在线一区二区 | 成人免费视频观看视频 | 九色www | 国产精品一区在线播放 | www.日韩| 日本天天操 | 日韩久久久久久 | 国产成人精品网站 | 国产成人精品午夜视频免费 | 亚洲黄色av| av黄色免费在线观看 | 国产成人免费 | 日韩精品免费一区 |