【问题标题】:Show all the data from table1 with matching records with table2 and show the result in to html table显示 table1 中的所有数据与 table2 匹配的记录,并将结果显示到 html 表中
【发布时间】:2018-05-03 10:44:22
【问题描述】:

如图所示,如果我想针对 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.");

            }
        },

    });
}

请帮忙解决这个问题。

【问题讨论】:

标签: javascript html mysql sql


【解决方案1】:

试试这个:

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

【讨论】:

  • 谢谢迪内什。上述解决方案是正确的,但在 UserID 列中显示空值,我需要根据 UserID 显示所有值。请看图片
猜你喜欢
  • 1970-01-01
  • 2020-08-12
  • 1970-01-01
  • 2020-07-18
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 2020-11-05
相关资源
最近更新 更多