简体   繁体   English

MySQL Join,即使不匹配…Laravel

[英]MySQL Join, even if there is not match … Laravel

Here is my Laravel query: 这是我的Laravel查询:

$all_tags = DB::table('tags')
        ->join('products', 'tags.product_id', '=', 'products.id')
        ->leftJoin('resources', 'tags.resource_id', '=', 'resources.id')
        ->where('tags.user_id', $user_id)
        ->select('tags.id as tag_id', 'tags.serial_number', 'tags.pin_number', 'tags.expiry_date', 'tags.active as tag_active', 'tags.activation_date', 'products.name', 'products.image', 'resources.name as resource_name')
        ->get();

The problem is, sometimes there is no matching product and sometimes there is no matching resource ... 问题是,有时没有匹配的product ,有时没有匹配的resource ...

How do I deal with the "select" statement in this case? 在这种情况下,如何处理“ select”语句?

如果即使在没有匹配的产品且没有匹配的资源的情况下也要返回标签,则需要在产品和资源表上都使用左连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM