MySQL Join,即使不匹配..拉拉维尔



这是我的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();

问题是,有时没有匹配的product有时没有匹配的resource......

在这种情况下,如何处理"选择"语句?

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

最新更新