oracle查询 大于行数,Laravel 4 - 选择连接表中行数大于0的行(Laravel 4 - Select rows where count of rows in joined table ...

Laravel 4 - 选择连接表中行数大于0的行(Laravel 4 - Select rows where count of rows in joined table greater than 0)

我需要抓住存在item行的user行。 这是我到目前为止的代码:

$users = DB::table('users')

->join('orders', 'orders.user_id', '=', 'users.id')

->join('items', 'items.user_id', '=', 'users.id');

如何添加仅选择至少包含一个item user的where条件?

I need to grab rows of users where rows of items exist. Here is my code so far:

$users = DB::table('users')

->join('orders', 'orders.user_id', '=', 'users.id')

->join('items', 'items.user_id', '=', 'users.id');

How do I add a where condition that will only select users that have at least one item?

原文:https://stackoverflow.com/questions/38022237

更新时间:2019-12-15 17:20

最满意答案

您不必添加额外条件。 查询将仅返回您拥有订单和相关项目的用户的结果。 如果您只想返回与项目相关的用户,可以使用orders表删除orders (即使我不知道您为什么在items表中而不是在orders表中使用user_id )。

$users = DB::table('users')->join('items', 'items.user_id', '=', 'users.id');

You don't have to add an extra condition. The query will return results ONLY for the users you have orders and items related with. If you want to return only the users that have items related with, you can drop the join with the orders table (even though I have no idea why you would have user_id in the items table instead of in the orders table).

$users = DB::table('users')->join('items', 'items.user_id', '=', 'users.id');

2016-06-24

相关问答

问题是你的WHERE子句过滤了“右手”表的值。 WHERE ...

AND (S.TYPE = 'RECEIPT' OR S.TYPE = 'INVOICE')

当外连接从左表生成“不匹配”行的记录时,它为右表中的所有列提供NULL值。 因此,对于那些记录, S.TYPE为NULL 。 有两种可能的解决方案: 明确允许WHERE逻辑中的“ NULL记录”情况。 根据某些标准,在将连接条件与过滤器分离时可能会“更纯粹”,但它可能会变得相当复杂(因此容易出错)。 需要注意的一个问题是,您可能必须

...

您不必添加额外条件。 查询将仅返回您拥有订单和相关项目的用户的结果。 如果您只想返回与项目相关的用户,可以使用orders表删除orders (即使我不知道您为什么在items表中而不是在orders表中使用user_id )。 $users = DB::table('users')->join('items', 'items.user_id', '=', 'users.id');

You don't have to add an extra condition. The query will

...

我认为这会起作用,但显然没有经过测试: $users = $this->prospectInstance->getDatabase()->table('Customer')

->join('WorkOrder', 'WorkOrder.customer_id', '=', 'Customer.customer_id')

->join('LineItems', 'LineItems.order_id', '=', 'WorkOrder.id')

->where('LineIt

...

从表a选择时使用row_number() (注意, a中行的顺序在over子句中定义): select a.*, b.*

from (

select row_number() over (order by id desc) as count, *

from a

) a

left join ab on a.id = ab.aid

left join b on b.id = ab.bid;

count | id | athing | id | bthing

...

我想你只是想在子查询中使用一个group by : SELECT u.id, u.code

FROM web_users u JOIN

(SELECT client_code

FROM web_users_branches

GROUP BY client_code

HAVING COUNT(*) = 1

) c

ON u.code = c.client_code;

I think you just want a group by

...

$groups = Group::where(['owner_id' => Auth::user()->id, 'year' => 2016])

->groupBy('status')

->select( DB::raw('status , COUNT(*) as status_count') )

->get();

Okay, here is what I've did: First I managed to create a correct SQL only with

...

你可以试试这个: $rows = DB::table('rows')->orderBy('id', 'desc')->take(5)->get();

如果该字段可用,您也可以使用orderBy('created_at') 。 这里也有类似的答案 。 You may try this: $rows = DB::table('rows')->orderBy('id', 'desc')->take(5)->get();

You may also use orderBy('created_at') i

...

您可以通过UNION ALL执行此操作。 注意: select语句中的列数应匹配 Select *

FROM (

SELECT * FROM A

UNION ALL

SELECT * FROM B

UNION ALL

SELECT * FROM C

)a

where value ='N'

更新: Select * From A

LEFT JOIN ( Select * FROM B INNER JOIN C

ON B.columnName = C.Column

...

MAX(ID)与计算行不同,因此排除了五个备选方案中的两个。 然后,您的任务是在剩余的三种方法之间进行性能比较以获得计数。 我认为实际上直接执行SQL语句可能会消除大量不必要的ORM层开销,实际上更快,但这将是过早的优化,除非事实证明。 MAX(ID) is not the same as counting rows, so that rules out two of five alternatives. And then it is your task to actually do a perf

...

您通常不使用连接来减少左表的结果集。 要限制结果集,请改用where子句 。 结合另一个表,这将是WHERE [NOT] EXISTS 。 所以让我们说这是你的主要问题: select * from main where main.col1 = 1;

它返回以下结果之一: 没有行,那我们就完成了 一排,然后我们也完成了 不止一行,那么我们必须扩展where子句 带有扩展where子句的查询: select * from main where main.col1 = 1

and exists (s

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值