【问题标题】:Query Execution [closed]查询执行[关闭]
【发布时间】:2016-07-15 14:42:39
【问题描述】:

如何在 laravel 中执行这个查询。 ?

  select d1.update_id from ( select update_id, count(update_id) 
  as ct from updates_tags where tag_id in (67,33,86,55) group by 
  update_id) as d1 where d1.ct=4

【问题讨论】:

  • 对于那些可以使用 whereRaw() 的查询类型
  • 没有得到结果。我想将它用于基于 t_id 的过滤器。如果多个 t_id 包含相同的 u_id 则应该打印。

标签: php mysql laravel laravel-4 laravel-5


【解决方案1】:

您可以用 eloquent 链接 where 条件,每个链接的条件都像 and 一样评估。

$update = Tag::whereIn('t_id',$tags)->whereIn('u_id',$tags)->where(/*more and conditions you could need*/)->lists('u_id')

编辑:如果您需要与 u_id 一致的,请考虑:

$update = DB::table('tags')
->select('t_id', 'u_id', DB::raw('count(*) as total'))
->whereIn('t_id',$tags)
->where('total','=',count($tags))
->groupBy('u_id')
->lists('u_id');

【讨论】:

  • 我想将它用于基于 t_id 的过滤器。如果多个 t_id 包含相同的 u_id 则应该打印。一次可能有多个 t_id 2 或 4 我该如何检查?
  • vivoconunxino:我想要 u_id 列表而不是 t_id。我是 laravel 的初学者。如果 t_id(11,21) 相同的 u_id 那么我需要那个 u_id。
  • 请检查并尝试编辑
  • vivoconunxino 它正在工作一半,但是当我只应用 t_id 时,它也显示其他 u_id 记录,甚至不包含该 u_id。
  • “我只申请 t_id”是什么意思?另外,您的表是否包含 u_id 为空的行?
【解决方案2】:

你可以在 Laravel 中使用原始查询

$results = DB::select(SELECT * FROM table WHERE id IN (1,2,3,4) AND );

http://fideloper.com/laravel-raw-queries

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 2022-12-02
    相关资源
    最近更新 更多