gpt4 book ai didi

mysql - 在选择MySQL中选择

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

我有两个查询,我想是否可以只在一个查询中作为 Select in Select 执行。

第一个:

SELECT
users.id
FROM users
LEFT JOIN users_date ON users_date.user = users.id
LEFT JOIN users_varchar ON users_varchar.user = users.id
WHERE
abilitato = 1
AND users_date.key = 'birthday'
AND users_varchar.key = 'nation'
AND users_varchar.value = 'US'
AND (users.reg_date >= '2013-05-31' AND users.reg_date <= '2013-05-31')
AND (floor(DATEDIFF(NOW(), users_date.value) / 365) >= 19 AND floor(DATEDIFF(NOW(), users_date.value) / 365) <= 19)

它检索用户 ID 列表(按年龄、国家或注册日期过滤)

第二个:

SELECT count(`matches`.`id`) FROM `matches` WHERE (`matches`.`status_home` = 3 AND `matches`.`status_guest` = 3) AND (`matches`.`team_home` = 13 OR `matches`.`team_guest` = 13)

我需要对 one 检索到的每个 ID 执行第二次选择。对于每次执行,我必须用检索到的 ID 替换值 13。

是否可以通过 select in select 在单个查询中执行所有操作?

提前感谢您的帮助

最佳答案

试试这个sql。

SELECT count(`matches`.`id`) 
FROM `matches` m
INNER JOIN ( SELECT users.id as id
FROM users
LEFT JOIN users_date ON users_date.user = users.id
LEFT JOIN users_varchar ON users_varchar.user = users.id
WHERE abilitato = 1
AND users_date.key = 'birthday'
AND users_varchar.key = 'nation'
AND users_varchar.value = 'US'
AND (users.reg_date >= '2013-05-31' AND users.reg_date <= '2013-05-31')
AND (floor(DATEDIFF(NOW(), users_date.value) / 365) >= 19 AND floor(DATEDIFF(NOW(), users_date.value) / 365) <= 19)) t
on t.id=m.team_guest
WHERE (`matches`.`status_home` = 3 AND `matches`.`status_guest` = 3)
AND (`matches`.`team_home` = 13 OR `matches`.`team_guest` = t.id)
GROUP BY t.id

关于mysql - 在选择MySQL中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16863511/

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