显示table1中与table2匹配记录的所有数据,并将结果显示到html表中

时间:2018-05-03 10:44:22

标签: javascript html mysql sql

enter image description here

如图像所示,如果我想针对UserID显示类似于结果表的结果,则Table1中有20条记录,Table2中有7条记录。

并将结果显示在数据库视图中。 我的观点代码是

<div class="tblContainer">
<table class="table table-striped table-bordered  " id="TblRole" cellspacing="0" align="center">
    <thead>
        <tr>
            <th>Roles</th>
            <th>CreateAccess</th>
            <th>ViewAccess</th>
            <th>EditAccess</th>
            <th>ReportAccess</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

我在jsUsers脚本中得到的数据是

function SearchUser() {
    var EmpCode = $('#EmpCode').val()
    alert("Call");
    $.ajax({
        type: "POST",
        url: "/Roles/GetUserRoleInformation",
        data: '{ EmpCode: "' + EmpCode + '" }',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.jsUsers != null) {

                alert("User Alredy Exist in FastTrack.");

                var rows;
                $.each(data.jsUsers, function (i, item) {
                    rows += "<tr>"
                              + "<td>" + item.RoleName + "</td>"
                              + "<td>" + item.CreateAccess + "</td>"
                              + "<td>" + item.ViewAccess + "</td>"
                              + "<td>" + item.EditAccess + "</td>"
                              + "<td>" + item.ReportAccess + "</td>"
                         + "</tr>";
                });
                $('#TblRole').append(rows);


            }
            else {
                alert("User Not Created yet.");

            }
        },

    });
}

请帮助解决此问题。

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT B.`UserID`, A.`RoleID`, A.`RoleName`, B.`Create`, B.`View`, B.`Edit`
FROM `Table1` A
LEFT JOIN `Table2` B ON A.`RoleID` = B.`RoleID` AND B.`UserID` = 1

更新答案:

只需将输入值用作UserID

SELECT 1 AS `UserID`, A.`RoleID`, A.`RoleName`, B.`Create`, B.`View`, B.`Edit`
FROM `Table1` A
LEFT JOIN `Table2` B ON A.`RoleID` = B.`RoleID` AND B.`UserID` = 1

否则,如果您将UserId作为parameter传递。

然后使用参数:

SELECT @ParamValue AS `UserID`, A.`RoleID`, A.`RoleName`, B.`Create`, B.`View`, B.`Edit`
FROM `Table1` A
LEFT JOIN `Table2` B ON A.`RoleID` = B.`RoleID` AND B.`UserID` = @ParamValue