Mysql语⾔select嵌套语句_如何在SQL中嵌套select语句?
5 个答案:
答案 0 :(得分:2)
⼦查询和加⼊适合您
SELECT DISTINCT u1.UserID
FROM
UserClicks u1
INNER JOIN
(
SELECT
UserID
FROM
UserClicks
WHERE
(Date > :monthBeforeStartDate AND Date < :startDate)
) u2
ON u1.UserID = u2.UserIDsql中select是什么意思
WHERE
(u1.Date > :startDate AND u1.Date < :endDate)
答案 1 :(得分:0)
如果你的查询挂起,那是因为索引,为了优化外部查询和内部查询,你需要像这样的索引:
create index du on UserClicks (Date, User_ID)
create index ud on UserClicks (User_ID, Date)
它应该⽴即返回。有关mysql如何优化使⽤IN运算符指定的⼦查询的更多详细信息:
MySQL取代了⼦查询
跟随索引查的表单
函数,EXPLAIN描述为
特殊连接类型(unique_subquery或
index_subquery):
... IN(SELECT indexed_column FROM
single_table ...)
答案 2 :(得分:0)
SELECT COUNT(DISTINCT User_ID) AS total
FROM UserClicks
WHERE (Date BETWEEN startDate AND endDate) AND User_ID IN (
SELECT User_ID
FROM UserClicks
WHERE (Date BETWEEN monthBeforeStartDate AND startDate)
) GROUP BY User_ID
答案 3 :(得分:0)
我在mysql中使⽤⼦查询很困难,其中两个查询都使⽤相同的表。我的解决⽅案⼀直是使⽤连接
SELECT COUNT(DISTINCT User_ID) AS total
FROM UserClick u1
JOIN UserClick u2
on u2.User_ID = u1.User_ID
WHERE u1.Date BETWEEN :startDate AND :endDate
AND u2.Date BETWEEN :monthBeforeStartDate and :startDate
答案 4 :(得分:0)
我很好奇......你为什么不在这⾥发表更详细的陈述?
SELECT COUNT(DISTINCT User_ID) AS total
FROM UserClicks
WHERE (Date > :startDate AND Date < :endDate)
AND (Date > :monthBeforeStartDate AND Date < :startDate)
更新:
您似乎想要计算在startDate之前点击的startDate和endDate之间点击的⽤户数量。
根据您的⽇期是否为MySQL友好格式(如YYYY-MM-DD HH:MM:SS),您可以使⽤MySQL获取开始⽇期之前的⽉份。⽰例如下:SELECT COUNT(DISTINCT User_ID) AS total
FROM UserClicks
WHERE (Date > :startDate AND Date < :endDate)
AND (Date > DATE_SUB(:startDate, INTERVAL 1 MONTH) AND Date < :startDate)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论