MySQL:使用AS变量查询不起作用

时间:2018-12-17 09:29:49

标签: php mysql sql sql-server subquery

我进行了查询,并使用它创建了一个变量accountId,为什么不能在子查询中重复使用accountId?

这是我的代码:

"data": [
    {
        "userId": 0,
        "accountId": 2,
        "fictive": false,
        "name": "Mickael",
        "phoneNumber": "0584688888",
        "review": "0",
        "liked": false,
        "recommended": 0,
        "requestStatus": 0,
        "recommendedTimes": 0
    }

输出示例:

final _url = "https://example.com/api/";
final _uri = Uri(path: _url, queryParameters: _params);

谢谢

2 个答案:

答案 0 :(得分:0)

检查此-

SELECT userId AS accountId,
        (SELECT recommended FROM ads_connections WHERE byUserId = userId AND throughUserId = userId AND adId = :recommendedAdId) AS recommended,
        --
        (SELECT requestStatus FROM ads_recommends_requests WHERE 
                                                                userId = :currentUserId AND 
                                                                requestFromUserId = userId AND 
                                                                advertisementId = :statusAdId) AS requestStatus,

        (SELECT COUNT(id) FROM ads_recommends_requests 
                WHERE 
                    requestFromUserId = userId AND 
                    advertisementId = :countAdId AND 
                    requestStatus = '1'
        ) AS recommendedTimes
        --
        FROM user_contacts

        WHERE userId IN " . $myCircleString . "
        AND phoneNumber = :phoneNumber
        AND (SELECT fictive FROM users WHERE id = userId) = 0
        ";

答案 1 :(得分:0)

答案答复

以这种方式创建变量:

SELECT @acctId := userId AS accountId

并使用它:

(SELECT requestStatus FROM ads_recommends_requests WHERE
                   userId = :currentUserId AND
                   requestFromUserId = @acctId AND
                   advertisementId = :statusAdId) AS requestStatus