首页 > 解决方案 > GraphQL Where Select 子查询

问题描述

这些天,我研究GraphQLLaravel框架,灯塔图书馆。我试过做那种SELECT Query

结果,我不知道 GraphQL 可以选择下面的 SQL Query

SELECT * FROM TABLE_A WHERE type=1 AND chart_id=(SELECT id FROM TABLE_B WHERE phone='0000~~')

我希望,客户端首先从这个查询中得到结果。

SELECT id FROM TABLE_B WHERE phone='0000~~'

然后做第二个查询,我想我可以得到一个结果。

但我想知道我能从 1 个请求中得到结果。谢谢。

标签: laravelgraphqllaravel-lighthouse

解决方案


您可以尝试以下

 $phoneNumber = '0000~~';

 $data = DB::table('tableA')->where('type',1)
            ->whereIn('chart_id',function($query) use ($phoneNumber) {
                $query->select('id')
                      ->from('tableB')
                     ->where('phone', '=',$phoneNumber);
         })->get();

如果两者之间有关系tableAtableB您可以执行以下操作

 TableA::where('type',1)
        ->whereHas('tableBRelationshipName', function ($q) use ($phoneNumber) {
             $q->select('id')
             $q->where('phone','=',$phoneNumber);
 })->get();

推荐阅读