mysql-Laravel连接表并连接行
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql-Laravel连接表并连接行,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3047字,纯文字阅读大概需要5分钟。
内容图文
所以我有两个表,组织和联系人.两个表都具有“电子邮件”列,我需要做的是保留组织名称,但是在电子邮件列中将所有电子邮件(组织的所有联系电子邮件)连接起来.
这是我没有运气尝试过的一些版本
1)这个不分组:
$customers = DB::table('customers')
->whereRaw('LENGTH(customers.email) > 4')
->select([
'customers.id',
'customers.name',
'customers.email'
]);
$contacts = DB::table('contacts')
->whereRaw('LENGTH(contacts.email) > 4')
->leftJoin('customers', 'contacts.customer_id', '=', 'customers.id')
->select([
'customers.id',
'customers.name',
'contacts.email'
]);
return $customers
->union($contacts)
->select([
'id',
'name',
DB::raw('GROUP_CONCAT(DISTINCT email, ", ") AS emails'),
])
->groupBy('id')
->get();
2)这实际上很接近,但是它不会过滤掉联系人或客户整体都没有DB :: raw(‘LENGTH(email)> 4’)的条目
return $customers = DB::table('customers')
->leftJoin('contacts', 'contacts.customer_id', '=', 'customers.id')
->select([
'customers.id',
'customers.name',
'registration',
DB::raw('GROUP_CONCAT(DISTINCT contacts.email, ", ") AS contact_emails'),
'customers.email'
])
->groupBy('customers.id')
->get();
3)我试图与子查询更加紧密(我知道它只会过滤掉没有电子邮件的联系人)
3.1)尝试子查询1导致错误:JoinClause :: whereRaw()不存在
return $customers = DB::table('customers')
->leftJoin('contacts', function($join) {
$join->on('contacts.customer_id', '=', 'customers.id')
->whereRaw('LENGTH(email) > 4');
})...
3.2)这会产生以下语法错误:
return $customers = DB::table('customers')
->leftJoin('contacts', function($join) {
$join->on('contacts.customer_id', '=', 'customers.id')
->where(DB::raw('LENGTH(email) > 4'));
})
1/2 PDOException in Connection.php line 333: SQLSTATE[42000]: Syntax
error or access violation: 1064 You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near ‘? group bycustomers
.id
‘ at line 12/2 QueryException in Connection.php line 713: SQLSTATE[42000]: Syntax
error or access violation: 1064 You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near ‘? group bycustomers
.id
‘ at line 1 (SQL:
selectcustomers
.id
,customers
.name
,registration
,
GROUP_CONCAT(DISTINCT contacts.email, “, “) AS contact_emails,
customers
.customers
left joincontacts
on
contacts
.customer_id
=customers
.id
and LENGTH(contacts.email)
4 group bycustomers
.id
)
3.3)一些示例说我应该这样做,但这会产生错误:on子句的参数不足.
return $customers = DB::table('customers')
->leftJoin('contacts', function($join) {
$join->on('contacts.customer_id', '=', 'customers.id');
$join->on(DB::raw('LENGTH(contacts.email) > 4'));
})
解决方法:
这对我有用.没有语法错误,并且筛选出长度少于4个字符的联系人:
DB::table('customers')
->leftJoin('contacts', function ($join) {
$join->on('contacts.customer_id', '=', 'customers.id')
->where(DB::raw('length(contacts.email)'), '>', 4);
})
->select([
'customers.id',
'customers.name',
DB::raw('group_concat(distinct contacts.email separator ", ") AS contact_emails'),
])
->groupBy('customers.id')
->get();
在Laravel 5.3.26,MySQL 5.6.20(无严格模式)中进行了测试.
内容总结
以上是互联网集市为您收集整理的mysql-Laravel连接表并连接行全部内容,希望文章能够帮你解决mysql-Laravel连接表并连接行所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。