gpt4 book ai didi

mysql - 查询的 WHERE 语句作为 @variable

转载 作者:行者123 更新时间:2023-11-29 19:29:33 25 4
gpt4 key购买 nike

我有一个查询,我想将变量插入到 WHERE 语句中。

哪里@变量

我已经尝试了以下(简化),但它似乎不起作用。

注意:在提出问题之前,我没有在此处包含串联元素,试图自己弄清楚该部分。

SET @id := x;
SET @n := (SELECT COUNT(*) FROM Table2 WHERE id=@id);
SET @Variable := (
(
Table1.Column1=(SELECT Column1 FROM Table2 WHERE id=@id LIMIT 1 OFFSET 0)
AND Table1.Column2=(SELECT Column2 FROM Table2 WHERE id=@id LIMIT 1 OFFSET 0)
AND Table1.Column3=(SELECT Column3 FROM Table2 WHERE id=@id LIMIT 1 OFFSET 0)
)
.........
OR
(
Table1.Column1=(SELECT Column1 FROM Table 2 WHERE id=@id LIMIT 1 OFFSET @n)
AND Table1.Column2=(SELECT Column2 FROM Table2 WHERE id=@id LIMIT 1 OFFSET @n)
AND Table1.Column3=(SELECT Column3 FROM Table2 WHERE id=@id LIMIT 1 OFFSET @n)
)
)
;


SELECT Table1.Column1, Table1.Column2, Table1.Column3, Table2.Column1, Table2.Column2, Table2.Column3
FROM Table1, Table2
WHERE
@Variable
;

最佳答案

所以这样:

SET @IDNumber := 21;
SELECT Players.PlayerID, COUNT(*) AS Games, SUM(Games.Points)
FROM Teams, Players, Games

WHERE
Teams.PlayerID=Players.PlayerID
AND
Games.Game=Teams.Game
AND
Games.Team=Teams.Team
AND
Games.GameDate=Teams.GameDate
AND
(
Games.Game= ANY (SELECT Game FROM Teams WHERE PlayerID=@IDNumber)
AND
Games.Team= ANY (SELECT Team FROM Teams WHERE PlayerID=@IDNumber)
AND
Games.GameDate= ANY (SELECT GameDate FROM Teams WHERE PlayerID=@IDNumber)
)

GROUP BY Teams.PlayerID
ORDER BY Games DESC
;

创建这个:

+----------+-------+-------------------+
| PlayerID | Games | SUM(Games.Points) |
+----------+-------+-------------------+
| 15 | 8 | 10 |
| 21 | 8 | 10 |
| 8 | 8 | 10 |
| 14 | 6 | 7 |
| 5 | 6 | 7 |
| 19 | 5 | 6 |
| 11 | 5 | 7 |
| 12 | 3 | 4 |
| 10 | 3 | 3 |
| 4 | 2 | 3 |
+----------+-------+-------------------+
10 rows in set (0.01 sec)

这正是我正在寻找的。

关于mysql - 查询的 WHERE 语句作为 @variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41896386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com