gpt4 book ai didi

mysql - 子查询作为 mysql 的来源

转载 作者:行者123 更新时间:2023-11-29 07:59:12 26 4
gpt4 key购买 nike

我在这里做错了什么?尝试使用子查询作为我的 FROM 以确保所有连接的表都连接到正确的集合上。

  SELECT
active_users.username as username,
active_users.computer_name as computer_name,
alert.cnt as alerts

FROM
(SELECT
computer_name,
username
FROM computers

INNER JOIN users
on users.computer_id = computers.computer_id

WHERE computers.account_id = :cw_account_id AND computers.status = :cw_status
) AS active_users

LEFT JOIN
(SELECT
user_id,
count(*) as cnt
from logs
group by user_id
) AS alert
on alert.user_id = active_users.user_id

最佳答案

您需要在第一个子查询中选择user_id:

 SELECT active_users.username as username, active_users.computer_name as computer_name,
alert.cnt as alerts
FROM (SELECT user_id, computer_name, username
FROM computers INNER JOIN
users
on users.computer_id = computers.computer_id
WHERE computers.account_id = :cw_account_id AND computers.status = :cw_status
) active_users LEFT JOIN
(SELECT user_id, count(*) as cnt
from logs
group by user_id
) alert
on alert.user_id = active_users.user_id;

关于mysql - 子查询作为 mysql 的来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24338309/

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