[英]Cant construct mysql select query without subqueries
I have a table 我有桌子
CREATE TABLE logs(
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
ip INT(11) UNSIGNED NOT NULL,
`date` TIMESTAMP,
`time` INT(11) UNSIGNED NOT NULL,
referrer VARCHAR(255) NOT NULL,
uri VARCHAR(255) NOT NULL,
PRIMARY KEY (id),
)
And I need to fetch following data and group then by IP: 我需要获取以下数据并按IP分组:
Is it possible to write such query without subqueries? 是否可以编写没有子查询的查询?
My solution WITH subqueries: 我的子查询解决方案:
SELECT
`logs`.`ip`,
(SELECT
`tmp`.`referrer`
FROM
`logs` AS `tmp`
WHERE
`tmp`.`ip` = `logs`.`ip`
ORDER BY `tmp`.`date`
LIMIT 1) 'referrer',
(SELECT
`tmp`.`uri`
FROM
`logs` AS `tmp`
WHERE
`tmp`.`ip` = `logs`.`ip`
ORDER BY
`tmp`.`date` DESC
LIMIT 1) 'uri',
COUNT(DISTINCT (`logs`.`uri`)) 'urls'
FROM
`logs`
GROUP BY
`logs`.`ip`
select
`logs`.`ip`,
`tmp`.`referrer`
`tmp`.`uri`
count(distinct `logs`.`uri`)) as `urls`
from `logs`
left join `tmp` on `tmp`.`ip`=`logs`.`ip`
group by `logs`.`ip`
However this doesn't allow for the order by tmp.date
part. 但是,这不允许order by tmp.date
部分order by tmp.date
。 If it's absolutely required, let me know in a comment and I'll update this code to allow that. 如果绝对需要,请在评论中让我知道,我将更新此代码以允许这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.