让我们考虑以下数据:
数据表1:
item1 item2
1 1
1 2
1 3
2 1
2 4
数据表2:
item1 nameItem1
1 fred
2 sam
我正在尝试编写一个javascript查询,该查询首先会根据table1
从item2
检索数据。
然后,它将使用该结果中的item1,并在表2中搜索名称
对于输出,我会得到以下结果:
item2 nameItem1
1 fred
1 sam
我正在使用parse.com作为我的数据库,并且在这方面失败了。以下是我的javascript代码示例:
var className = "seminarAttendance"; //change this to the appropriate class name
var query = new Parse.Query(className);
query.equalTo("StudentID", attendancesearch.studentID.value);
query.find({
success: function(results) {
if(results.length==0){
alert("Sorry, there are no results that match your search.")
{return;}}
var w = window.open();
w.document.write('<html><head><title>Result Table</title>');
w.document.write('<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" type="text/css" >');
w.document.write('<link rel="stylesheet" href="tablesearch.css" type="text/css" />');
w.document.write(' </head><body><h3> Here are your search results!</h3><br/>');
w.document.write(' Student # ' + attendancesearch.studentID.value + ' has attended ' +results.length + ' seminars <br>');
w.document.write('<div class="search"><table class="table table-bordered table-condensed table-striped" >');
w.document.write('<thead><tr><td><b>Seminar ID</b></td><td><b>Student ID</b></td><td><b>Time Period</b></td><td><b>Student ID</b></td><td><b>Student ID</b></td></tr></thead><tbody>');
for (var i=0; i < results.length; i++){
var object = results[i];
var semid = object.get("SeminarID");
var studid=object.get("StudentID");
var timeper=object.get("timePeriodID");
var semclass = "seminarTable";
var query2 = Parse.Query(semclass);
query2.equalTo("seminarID", semid);
query2.first({
success: function(q2object){
var test = q2object.get("seminarName");
},
});
w.document.write('<tr>');
w.document.write('<td>' + semid + '</td>');
w.document.write('<td>' + studid + '</td>');
w.document.write('<td>' + timeper + '</td>');
w.document.write('<td>' + test + '</td>');
w.document.write('</tr>');
}
w.document.write('</tbody></table></div></body></html>');
w.document.close();
答案 0 :(得分:0)
您不能在&#39;中使用&#39;循环。因为查询需要花费一些时间,这比执行&#39;要大得多。环。 相反,你可以获取&#34; seminarTable&#34;的所有对象。将它们存储在另一个数组中(比如results1)。 现在我们有结果和结果1数组,因此您可以手动搜索并放置条件。
var className = "seminarAttendance"; //change this to the appropriate class name
var query = new Parse.Query(className);
query.equalTo("StudentID", attendancesearch.studentID.value);
query.find({
success: function(results) {
if(results.length==0){
alert("Sorry, there are no results that match your search.")
{return;}}
var w = window.open();
w.document.write('<html><head><title>Result Table</title>');
w.document.write('<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" type="text/css" >');
w.document.write('<link rel="stylesheet" href="tablesearch.css" type="text/css" />');
w.document.write(' </head><body><h3> Here are your search results!</h3><br/>');
w.document.write(' Student # ' + attendancesearch.studentID.value + ' has attended ' +results.length + ' seminars <br>');
w.document.write('<div class="search"><table class="table table-bordered table-condensed table-striped" >');
w.document.write('<thead><tr><td><b>Seminar ID</b></td><td><b>Student ID</b></td><td><b>Time Period</b></td><td><b>Student ID</b></td><td><b>Student ID</b></td></tr></thead><tbody>');
var semclass = "seminarTable";
var query2 = Parse.Query(semclass);
query2.find({
success: function(results1){
//Do whatever you want here
w.document.write('<tr>');
w.document.write('<td>' + semid + '</td>');
w.document.write('<td>' + studid + '</td>');
w.document.write('<td>' + timeper + '</td>');
w.document.write('<td>' + test + '</td>');
w.document.write('</tr>');
},
});
}
w.document.write('</tbody></table></div></body></html>');
w.document.close();