Laravel MySQL加入问题

时间:2018-08-18 10:36:07

标签: php mysql laravel laravel-5

我有2个表,两个表的字段名和数据类型都完全相同。

问题: 1)我无法从两列接收数据 2)我还需要在两个表中都计算电话号码

字段名称是member_id,name,phone

`SAMPLE DATA IN MEMBER TABLE`
------------------------------
member_id | name | phone     |
------------------------------
100000    | ABC | 9876543210 |
-----------------------------*

`NOW SAMPLE DATA IN MEMBER_TEMP TABLE`
------------------------------
member_id | name | phone     |
------------------------------
100001    | DEF | 9876543210 |
-----------------------------*

`NOW EXPECTED RESULTS/ OUTPUT EXPECTED`
------------------------------
member_id | name | phone     |
------------------------------
100000    | ABC | 9876543210 |
------------------------------
100001    | DEF | 9876543210 |
-----------------------------*

下面是LARAVEL QUERY Builder

$member_info = DB::table('member')->select('member_temp.*','member.*')->where(
            array(
            )
    );

    $member_info = $member_info->join('member_temp','member_temp.phone', '=', 'member.phone')
            ->get()->toArray();

// RAW MYSQL查询

select *
from `member`
inner join `member_temp`
    on `member_temp`.`phone` = `member`.`phone`

感谢前进

2 个答案:

答案 0 :(得分:1)

我建议您应该使用雄辩的模型从两个表中获取电话列。由于相同的列名,在流利的查询(联接)中将始终发生冲突

答案 1 :(得分:1)

我认为您是从错误的原始sql查询开始的。

您似乎正在寻找更多类似的东西来从数据库中获取所需的所有数据:

select a.memberid as MemberId, b.memberid as MemberTempId, a.name as MemberName, 
b.name as MemberTempName, a.phone from member as a join member_temp as b on 
member.phone = member_temp.phone;