首页 > 解决方案 > 如何加入“IN”结果并检查“where” active = 1?

问题描述

在我的查询中,我检查是否tokens.id存在于dapp_tokens. 我想补充一点,结果dapp_tokens也必须active = 1dapps表格中。

所以我需要加入在中找到dapps的行的表,但是如何在 Raw SQL 中做到这一点?tokens.iddapp_tokens

$data['tokens'] = Token::where('active', 1)
    ->whereRaw('tokens.id in (select token_id from dapp_tokens where active = ?)', [1])
    ->sortable('market_cap')
    ->orderby('id','desc')
    ->paginate($page_count);

标签: phpmysqlsqllaravellaravel-5

解决方案


我试过这个,但我只能用“IN”获得 1 col 值:

基数违规:1241 操作数应包含 1 列

        $data['tokens'] = Token::where('tokens.active', 1)
            ->whereRaw('tokens.id in (select token_id, dapp_id from dapp_tokens LEFT JOIN dapps ON dapps.id = dapp_id where dapps.active = ?)', [1])
            ->sortable('market_cap')->orderby('id','desc')->paginate($page_count);

编辑:好像我已经有了解决方案,只需要从 whereRaw 的选择器中删除 dapp_id


推荐阅读