首页 > 解决方案 > 显示 table1 中的所有数据与 table2 匹配的记录,并将结果显示到 html 表中

问题描述

在此处输入图像描述

如图所示,如果我想针对 UserID 显示结果表等结果,我有 20 条记录Table1和 7条记录。Table2

并将结果从数据库中显示到视图中。我的视图代码是

<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.");

            }
        },

    });
}

请帮助解决这个问题。

标签: javascripthtmlmysqlsql

解决方案


尝试这个:

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

否则,如果您通过UserIdas 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

推荐阅读