[英]MySQL loop and multiple LEFT joins
I got the following code: 我得到以下代码:
SELECT
COALESCE(rv.views, 0) as views
FROM
( select 0 as n
union all select 1
union all select 2
union all select 3 ) n
LEFT JOIN restaurant_views rv
on rv.date = date_add("2015-02-24", interval - n.n day)
and restaurant_id = 192
This code is giving me the amount of views a restaurant had the last 4 days. 此代码为我提供了餐厅最近4天的观看次数。
I am looking for a similar query to get the amount of likes a restaurant had the last 4 days. 我正在寻找一个类似的查询,以获取过去4天餐厅的喜欢人数。
This is what I got so far: 这是我到目前为止所得到的:
SELECT
( COUNT( DISTINCT a.restaurant_id)
+ COUNT( DISTINCT d.restaurant_id)) as num_likes
FROM
( select 0 as n
union all select 1
union all select 2
union all select 3 ) n
LEFT JOIN apple_likes a
on a.vote_date = date_add("2015-02-24", interval - n.n day)
and a.restaurant_id = 192
LEFT JOIN android_likes d
on d.vote_date = date_add("2015-02-24", interval - n.n day)
and d.restaurant_id = 192
And here is the output, which is as you can see not what I'm looking for: 这是输出,正如您所看到的,不是我在寻找什么:
What do I have to change to get the number of likes in the last query? 要在上一次查询中获得喜欢的次数,我必须更改什么?
(I have checked that the restaurant has likes on all days, so I am positive it's something wrong with the query) (我检查了餐厅整天都喜欢,所以我肯定查询有问题)
Try this one: 试试这个:
SELECT
( a.likes)
+ d.likes) as num_likes
FROM
( select 0 as n
union all select 1
union all select 2
union all select 3 ) n
LEFT JOIN (
SELECT vote_date,COUNT(*) as likes
FROM apple_likes
WHERE restaurant_id = 192
GROUP BY restaurant_id, vote_date
) as a
on a.vote_date = date_add("2015-02-24", interval - n.n day)
LEFT JOIN (
SELECT vote_date, COUNT(*) as likes
FROM android_likes
WHERE restaurant_id = 192
GROUP BY restaurant_id, vote_date
) as d
on d.vote_date = date_add("2015-02-24", interval - n.n day)
I can think of a couple items that might be what you are encountering... 我能想到的可能是您遇到的一些问题...
Just because somebody VIEWS a restaurant, does that mean they actually VOTED??? 仅仅因为有人看过餐厅,是否就意味着他们实际上投票了??? And if Voted, are the only two devices that of apple or android? 如果投票的话,苹果或android仅有的两个设备是吗? What if viewing from a browser and they are on a Windows machine browser-based? 如果从浏览器查看并且它们位于基于Windows计算机的浏览器上怎么办?
Date Equality. 日期平等。 In the restaurant views table, is the date field ALWAYS that of a time = 12:00:00 (ie: midnight/morning of the day). 在餐厅视图表中,日期字段始终为时间= 12:00:00(即:午夜/当天早上)。 If the time-stamps of the votes are anything other than 12:00:00, and you are trying to compare for a date = date + time is probably failing. 如果表决的时间戳记不是12:00:00,则您正在尝试比较日期=日期+时间可能失败。 What you may need is a comparison of the date( vote_date ) = date( date_add( ... )) so this way BOTH are ignoring the time component... Now, that being said, a function on a date column is not going to be optimized, even if the restaurant ID is numeric and part of the index key... it would be PARTIALLY optimized. 您可能需要比较一下date(vote_date)= date(date_add(...)),所以这两种方式都忽略了时间分量……现在,这么说,date列上的函数不会运行即使餐厅ID是数字并且是索引键的一部分,也可以进行优化...它也会被部分优化。 You may want to just add a generic date of AND vote_date >= '2015-02-20' so it can optimize the restaurant and date, then apply the DATE( vote_date ) for the actual qualfying of records. 您可能只想添加AND投票日期> ='2015-02-20'的通用日期,以便它可以优化餐厅和日期,然后将DATE(投票日期)应用于记录的实际资格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.