SQL在类似的coloumn上加入原始查询问题

时间:2016-05-21 12:18:49

标签: php mysql sql laravel join

这是我的报告表

user
id name    user_type
1  raj     1
3  ram     1
4  anthony 2
6  kumar   2

用户表是

$data = Report::select('user.name as user_name')
                    ->leftjoin('users','users.id','=','report.user_id')
                    ->get();

这里用户名为user_type 1,field_tech为

我如何加入并获取订单的用户名和字段技术名称

我试过

$data = Report::select('user.name as user_name')
                    ->leftjoin('users','users.id','=','report.user_id')
                    ->leftjoin('users','users.id','=','report.field_tech_id')
                    ->get();

但是当我尝试

<form id="checkserver" method="POST">

<select class="btn2" name="server" onchange="$('#checkserver').submit();">
    <option value="all">Show all </option>
    <option value="Russia" <?=(isset($_POST["server"]) && $_POST["server"] == 'Russia')?'selected':''?>>Russia</option>
    <option value="Europe" <?=(isset($_POST["server"]) && $_POST["server"] == 'Europe')?'selected':''?>>Europe</option>
    <option value="Indonesia" <?=(isset($_POST["server"]) && $_POST["server"] == 'Indonesia')?'selected':''?>>Indonesia</option>

    </select>
</form>

如何获取用户名和字段技术名称?

2 个答案:

答案 0 :(得分:2)

试试这个:

$data = Report::select('user.name as user_name')
                ->leftjoin('users as users1','users.id','=','report.user_id')
                ->leftjoin('users as users2','users.id','=','report.field_tech_id')
                ->select('users1.*,users2.*)                    
                ->get();

答案 1 :(得分:0)

如果你可以传递整个SQL查询,那么这应该有效:

SELECT u.name as user_name, r.field_tech_id
FROM user u
LEFT JOIN report r
ON u.id = r.user_id