[英]CakePHP 3 - How do I use subquery in IN condition?
I have to build the following query but don't have idea in the select max() part. 我必须建立以下查询,但在select max()部分中不知道。
Having these 2 tables: 有这两个表:
players 玩家
-------------
| id | name |
-------------
| 1 | AAAA |
-------------
| 2 | BBBB |
-------------
scores 分数
----------------------------------------
| player_id | score | created |
----------------------------------------
| 1 | 1080 | 2015/12/28 15:38 |
----------------------------------------
| 1 | 1199 | 2015/12/28 16:00 |
---------------------------------------|
| 2 | 2400 | 2015/12/29 10:30 |
----------------------------------------
| 2 | 2420 | 2015/12/29 10:41 |
----------------------------------------
| 2 | 2031 | 2015/12/29 11:12 |
----------------------------------------
Result I would like to have: 结果我希望拥有:
----------------------------
| id | name | latest_score |
----------------------------
| 1 | AAAA | 1199 |
----------------------------
| 2 | BBBB | 2031 |
----------------------------
SQL Query #1 - This query gives the above result I want SQL查询#1-此查询给出我想要的上述结果
SELECT
players.id AS id,
players.name AS name,
scores.player_id,
scores.score AS latest_score,
scores.created
FROM
players
JOIN scores ON
players.id = scores.player_id
WHERE
scores.created IN(
SELECT MAX(scores.created)
FROM
scores
WHERE
scores.player_id= player.id
)
SQL Query #2 - By @tim-biegeleisen, works too, so or you may help me to build this query? SQL查询#2-通过@ tim-biegeleisen,也可以使用,所以还是可以帮助我建立此查询?
SELECT p.id, p.name, s1.score AS latest_score
FROM players p INNER JOIN scores s1
ON p.id = s1.player_id
INNER JOIN
(
SELECT s.player_id, MAX(s.created) AS latest
FROM scores s
GROUP BY s.player_id
) s2
ON s1.player_id = s2.player_id AND s1.created = s2.latest
Currently trying to build something like: 目前正在尝试建立类似:
$query = $this->Players->find()
->select([
"Players.id",
"Players.name",
"Scores.scores",
"Scores.created"
])
->join([
"Scores" => [
"table" => "scores",
"type" => "left",
"conditions" => "Players.id = Scores.player_id"
]
])
->where([
"Scores.created" =>
$this->Players->Scores->find()
->select(["MAX(Scores.created)"])
->where(["Scores.player_id = Players.id"])
]);
Question is, this builder doesn't work: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 问题是,此生成器不起作用:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;可能有错误。 check the manual that corresponds to your MariaDB server version for the right syntax to use near 'MAX(`Scores__created`) FROM scores Scores WHERE Scores.player_id = Players.i' at line 1 检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在第1行的'MAX(`Scores__created`)FROM scores Scores WHERE Scores.player_id = Players.i'附近使用
I don't want to just build a join and then order the Scores.created
desc, and grab the [0]
to get the first row. 我不想只是建立一个Scores.created
,然后订购Scores.created
desc,并抓住[0]
来获得第一行。 You know how to build this query? 您知道如何建立此查询吗? Please help. 请帮忙。
Thank you! 谢谢!
Try using the CakePHP SQL function syntax: 尝试使用CakePHP SQL函数语法:
$subquery = $this->Players->Scores->find();
$query = $this->Players->find()
->select([
"Players.id",
"Players.name",
"Scores.scores",
"Scores.created"
])
->join([
"Scores" => [
"table" => "scores",
"type" => "left",
"conditions" => "Players.id = Scores.player_id"
]
])
->where([
"Scores.created" => $subquery
->select([$subquery->func()->max('Scores.created')])
->where(["Scores.player_id = Players.id"])
]);
You can read the docs on the ORM's SQL functions for CakePHP 3 here. 您可以在此处阅读关于CakePHP 3的ORM SQL函数的文档。
I am not a CakePHP expert, but I can offer you the following join query which avoids the subquery in the WHERE
clause. 我不是CakePHP专家,但是我可以为您提供以下联接查询,该查询避免了WHERE
子句中的子查询。 Maybe this will help you find a way around the problem you are having. 也许这可以帮助您找到解决问题的方法。
SELECT p.id, p.name, s1.score AS latest_score
FROM players p INNER JOIN scores s1
ON p.id = s1.player_id
INNER JOIN
(
SELECT s.player_id, MAX(s.created) AS latest
FROM scores s
GROUP BY s.player_id
) s2
ON s1.player_id s2.player_id AND s1.created = s2.latest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.