[英]Mysql Query for fetching records using single Query from three tables
We have three table我们有三张桌子
table 1- app ( id , name )表 1- 应用程序( id , name )
table 2- appPlayer ( id , name )表 2- appPlayer ( id , name )
table 3- appPlayerSession ( id , appId , appPlayerId , version)表 3- appPlayerSession ( id , appId , appPlayerId , version)
my Current query is:我当前的查询是:
SELECT (select name from app k where k.id= aps.appId) AS appName,version,appId,count(version) FROM appPlayerSession aps GROUP BY appId,version,appName
we need to count the session users for each game with same version, and also woth the object of all users data using single mysql query.我们需要统计每个版本相同的游戏的会话用户数,并且还需要使用单个mysql查询来获取所有用户数据的对象。
Current Result using my query, but we also need players for each app..使用我的查询的当前结果,但我们还需要每个应用程序的播放器..
As you havent given your expected result and on basis of your requirement you can do something this.it may be enhanced as per your requirement.由于您没有给出您的预期结果,并且根据您的要求,您可以执行此操作。它可能会根据您的要求进行增强。
SELECT (select name from app k where k.id= aps.appId) AS appName,version,appId,(select P.name from appPlayer P where P.id=aps.appPlayerid) as appPlayerName, count(version) FROM appPlayerSession aps GROUP BY appId,version,appName,appPlayerName
Also check fiddle as per your requirement created as you havent given any data set and its on my assumption.还要根据您创建的要求检查小提琴,因为您没有提供任何数据集及其基于我的假设。 http://sqlfiddle.com/#!9/30fe4f/1 http://sqlfiddle.com/#!9/30fe4f/1
New Sql as per your new added requirement-根据您的新添加要求新建 Sql-
select X.appname,X.version,X.appid,GROUP_CONCAT(distinct X.appPlayerName order by X.appPlayerName) as Users ,
sum(X.vercount)
from (SELECT (select name from app k where k.id= aps.appId)
AS appName,version,appId,
(select P.name from appPlayer P where P.id=aps.appPlayerid)
as appPlayerName, count(version)as vercount
FROM appPlayerSession aps
GROUP BY appId,version,appName,appPlayerName) X
group by X.appname,X.version,X.appid
New fiddle - http://sqlfiddle.com/#!9/13646c/5新小提琴 - http://sqlfiddle.com/#!9/13646c/5
You can use JOIN in sql to connect with multiple tables and fetch result您可以在sql中使用JOIN连接多个表并获取结果
Below is the format :以下是格式:
SELECT t1.col,
t3.col
FROM table1
JOIN table2
ON table1.primarykey = table2.foreignkey
JOIN table3
ON table2.primarykey = table3.foreignkey
In your case :在你的情况下:
SELECT app.col,
appPlayer.col,
appPlayerSession.col
FROM app
JOIN appPlayer
ON app.id = appPlayer.appId
JOIN appPlayerSession
ON appPlayer.id = appPlayerSession.appPlayerId
Hope this is helpful.希望这是有帮助的。
One suggestion .一个建议。 It is not a standard to use camelCase for table and column names.将驼峰命名用于表名和列名不是标准。 snake_case is preferred widely. snake_case 是广泛首选的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.