gpt4 book ai didi

php - 使用 Laravel Eloquent/MySQL 比较多个数据库行值

转载 作者:行者123 更新时间:2023-11-30 21:50:49 24 4
gpt4 key购买 nike

我正在为我的项目使用 Laravel,并且有一个 MySQL 表如下:

| User_id | technology | years_experience |
| 1 | PHP | 4 |
| 2 | AngularJS | 2 |
| 1 | HTML | 2 |

目前我正在使用仅支持一个条件的标准 where 查询

Table::where([
['technology', '=', 'PHP'],
['years_experience', '>', 2]
])->get()->pluck('user_id');

如果我想使用 PHP > 2 和 HTML > 1 检索所有 User_id,如何按 User_id 过滤和分组结果?

最佳答案

使用 WHERE/AND 的问题是它不会在行级别比较值,而是跨列搜索匹配值

“技巧”是使用 HAVING()。这是工作解决方案:

Table::groupBy('user_id')
->havingRaw("sum(tech='PHP' AND experience>=2) > 0")
->havingRaw("sum(tech='HTML' AND experience>=1) > 0")
->get()
->pluck('user_id')

关于php - 使用 Laravel Eloquent/MySQL 比较多个数据库行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47362297/

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