javascript - 显示 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.");
}
},
});
}
请帮助解决这个问题。
解决方案
尝试这个:
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
as 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
推荐阅读
- android - 创建存储库对象时出现实例化错误“java.lang.InstantiationError:”
- javascript - javascript对象的转换和删除一些字段
- javascript - ThreeJS 在物体靠近时将物体粘到另一个物体的边缘(如磁力拖动效果)
- java - JDBC顺序PreparedStatement?
- python - 这段代码是一个爱计算器的代码我怎样才能简化代码?
- javascript - 反应路由器使用参数返回空对象
- c# - 使用 C# openXml 在 Excel 文件中插入图像
- android - Android RegisterForActivity 不返回 Uri
- javascript - 停止计划的推送通知在反应本机推送通知中
- java - Eclipse Java 更新