问题描述
need to display the contents of a google sheet (rows & columns) on a html page (not using google web app),and sadly data not being displayed as a html page??
the code loads the google sheet files,but no content is displayed???
We have used code from following URL:
https://www.w3schools.com/xml/xml_http.asp
note:
a/ have made public and publish the google sheet contents
a.1/ using google drive,obtain following link:
https://docs.google.com/spreadsheets/d/1HVmBfKjQiUyXOfy-q5iWVvDYOSKnMLPiDr18W2EtU9s/edit?usp=sharing
a.2/ publish google sheet to the web
https://docs.google.com/spreadsheets/d/e/2PACX-1vQ3ZHpAYDBhjSelXk-GFuFJACQzsqlufZ0d5UCLw8iJNJwdHglY7388fYHL4632wgXDIfgnrd238Htg/pubhtml
<!DOCTYPE html>
<html>
<body>
<h2>Using the amstras XMLHttpRequest Object</h2>
<div id="demo">
<button type="button" onclick="loadXMLDoc()">Load Data</button>
</div>
<script>
function loadXMLDoc() {
var xhttp = new XMLHttpRequest();
var vUrl = "https://docs.google.com/spreadsheets/d/e/2PACX-1vQ3ZHpAYDBhjSelXk-GFuFJACQzsqlufZ0d5UCLw8iJNJwdHglY7388fYHL4632wgXDIfgnrd238Htg/pubhtml?gid=0&single=true&output=csv";
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("GET",vUrl,true);
xhttp.send();
}
</script>
</body>
</html>
我正在尝试:
select distinct m.id,m.name,sch.id as schedule,COUNT(tk.id) as tiketSold
from movies m,schedules sch,tickets tk
where m.id = sch.movie_id and sch.id = tk.schedule_id
group by m.id,sch.id
order by COUNT(tk.id) desc
解决方法
我没有你的数据库,所以,我已经按照我的预期创建了 3 个像你的表一样的类。然后我在“TestMethod”中创建了 3 个像你一样的列表。在 linq 查询中,我加入了 3 个列表,如您在 sql 查询段“where m.id = sch.movie_id and sch.id = tk.schedule_id”中所示,然后我执行 group by,按 select 排序。这是我的代码,请尝试一下,让我知道它是否有效。
public class movies
{
public int id { get; set; }
public string name { get; set; }
}
public class schedules
{
public int id { get; set; }
public int movie_id { get; set; }
}
public class tickets
{
public int id { get; set; }
public int schedule_id { get; set; }
}
void TestMethod()
{
//Add Movies to the list
List<movies> moviesItems = new List<movies>();
moviesItems.Add(new movies() { id = 1,name = "A" });
moviesItems.Add(new movies() { id = 2,name = "B" });
//Add Schedules to the list
List<schedules> schedulesItems = new List<schedules>();
schedulesItems.Add(new schedules() { id = 1,movie_id = 1 });
schedulesItems.Add(new schedules() { id = 2,movie_id = 2 });
schedulesItems.Add(new schedules() { id = 3,movie_id = 1 });
schedulesItems.Add(new schedules() { id = 4,movie_id = 2 });
//Add Tickets to the list
List<tickets> ticketsItems = new List<tickets>();
ticketsItems.Add(new tickets() { id = 1,schedule_id = 1 });
ticketsItems.Add(new tickets() { id = 2,schedule_id = 1 });
ticketsItems.Add(new tickets() { id = 3,schedule_id = 2 });
ticketsItems.Add(new tickets() { id = 4,schedule_id = 2 });
ticketsItems.Add(new tickets() { id = 5,schedule_id = 2 });
ticketsItems.Add(new tickets() { id = 6,schedule_id = 3 });
ticketsItems.Add(new tickets() { id = 7,schedule_id = 3 });
ticketsItems.Add(new tickets() { id = 8,schedule_id = 3 });
ticketsItems.Add(new tickets() { id = 9,schedule_id = 3 });
ticketsItems.Add(new tickets() { id = 10,schedule_id = 4 });
var query = from final in (from m in moviesItems
join sch in schedulesItems on m.id equals sch.movie_id
join tk in ticketsItems on sch.id equals tk.schedule_id
select new { movieID = m.id,movieName = m.name,schID = sch.id,tkID = tk.id })
group final by new { final.movieID,final.movieName,final.schID } into g
orderby g.Count() descending
select new { g.Key.movieID,g.Key.movieName,g.Key.schID,tiketSold = g.Count() };
}
,
此查询最接近您的 SQL,但您可能需要 LEFT JOIN。如果您提供模型,也可以使用导航属性进行简化。
var hotMovie =
from m in _db.movies
join sch in _db.schedules on m.id equals sch.movie_id
join tk in _db.tickets on sch.id equals tk.schedule_id
group tk by new { movieID = m.id,scheduleId = sch.id } into g
orderby g.Sum(x => x.id != null ? 1 : 0) descending
select new
{
g.Key.movieID,g.Key.scheduleId,tiketSold = g.Sum(x => x.id != null ? 1 : 0)
};