gpt4 book ai didi

php - MySQL 加入,即使没有匹配... Laravel

转载 作者:行者123 更新时间:2023-11-29 00:25:45 28 4
gpt4 key购买 nike

这是我的 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 ...

在这种情况下如何处理“select”语句?

最佳答案

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

关于php - MySQL 加入,即使没有匹配... Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19017033/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com