计算同一会员表中 2 个日期之间的推荐人新会员

发布于 2024-12-13 19:40:15 字数 1110 浏览 0 评论 0原文

我现在正在进行的项目需要一个参考系统(目前他们有 50K 成员)。我决定在成员表中添加 refref_id 字段。

members表的结构;

id (int auto),
admin (enum (1,0)),
ref (enum (1,0)),
ref_id (int),
country_id (int),
city_id(int),
town_id(int),
totalRef (int),
fullName (varchar),
registrationDate (datetime)

我想列出两个日期之间有新成员的推荐人数据。我想提供更多详细信息,因此我还尝试在查询中添加国家/地区、城市和城镇。我尝试了以下查询,但考虑到加载需要很长时间,我认为这不是一个好方法;

SELECT m.id, m.fullName, m.country_id, m.city_id, m.town_id, m.totalRef,
(select name from country where country.id = m.country_id) as countryName,
(select name from city where city.id = m.city_id) as cityName,
(select name from town where town.id = m.town_id) as townName,
(select count(id) from members where members.ref_id = m.id AND ref_id > 0 AND registrationDate BETWEEN '2011.11.04 00:00:00' AND '2011.11.04 23:59:59') as newRef

FROM members as m 

WHERE

m.country_id = '224' AND 
m.city_id = '4567' AND
m.town_id = '78964' AND 
m.admin = '0' AND 
m.ref = '1' 

ORDER BY newRef DESC 
LIMIT 0, 25

如果您能帮助我解决这个问题,我将很高兴。先感谢您。

The project I'm working on right now needs a reference system (currently they have 50K members). I decided to add ref and ref_id field in members table.

Structure of members table;

id (int auto),
admin (enum (1,0)),
ref (enum (1,0)),
ref_id (int),
country_id (int),
city_id(int),
town_id(int),
totalRef (int),
fullName (varchar),
registrationDate (datetime)

I would like to list referers data which has new members between 2 dates. I wanted to provide a bit more details so I also tried to add country, city, and town in the query. I tried following query but I don't think this is a good approach to go with considering it takes really long time to load ;

SELECT m.id, m.fullName, m.country_id, m.city_id, m.town_id, m.totalRef,
(select name from country where country.id = m.country_id) as countryName,
(select name from city where city.id = m.city_id) as cityName,
(select name from town where town.id = m.town_id) as townName,
(select count(id) from members where members.ref_id = m.id AND ref_id > 0 AND registrationDate BETWEEN '2011.11.04 00:00:00' AND '2011.11.04 23:59:59') as newRef

FROM members as m 

WHERE

m.country_id = '224' AND 
m.city_id = '4567' AND
m.town_id = '78964' AND 
m.admin = '0' AND 
m.ref = '1' 

ORDER BY newRef DESC 
LIMIT 0, 25

I will be glad if you could help me about this problem. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2024-12-20 19:40:15

像这样的东西 -

SELECT
  m.id,
  m.fullName,
  m.country_id,
  m.city_id,
  m.town_id,
  m.totalRef,
  cnt.name countryName,
  ct.name cityName,
  t.name townName,
  m2.newRef
FROM members as m 
  LEFT JOIN country cnt
    ON cnt.id = m.country_id
  LEFT JOIN city ct
    ON ct.id = m.city_id
  LEFT JOIN town t
    ON t.id = m.town_id
  LEFT JOIN (
    SELECT ref_id, COUNT(id) newRef FROM members
      WHERE ref_id > 0 AND registrationDate BETWEEN '2011.11.04 00:00:00' AND '2011.11.04 23:59:59'
      GROUP BY ref_id
    ) m2
    ON m2.ref_id = m.id
WHERE
  m.country_id = '224' AND 
  m.city_id = '4567' AND
  m.town_id = '78964' AND 
  m.admin = '0' AND 
  m.ref = '1' 
ORDER BY
  newRef DESC 
LIMIT
  0, 25;

Something like this -

SELECT
  m.id,
  m.fullName,
  m.country_id,
  m.city_id,
  m.town_id,
  m.totalRef,
  cnt.name countryName,
  ct.name cityName,
  t.name townName,
  m2.newRef
FROM members as m 
  LEFT JOIN country cnt
    ON cnt.id = m.country_id
  LEFT JOIN city ct
    ON ct.id = m.city_id
  LEFT JOIN town t
    ON t.id = m.town_id
  LEFT JOIN (
    SELECT ref_id, COUNT(id) newRef FROM members
      WHERE ref_id > 0 AND registrationDate BETWEEN '2011.11.04 00:00:00' AND '2011.11.04 23:59:59'
      GROUP BY ref_id
    ) m2
    ON m2.ref_id = m.id
WHERE
  m.country_id = '224' AND 
  m.city_id = '4567' AND
  m.town_id = '78964' AND 
  m.admin = '0' AND 
  m.ref = '1' 
ORDER BY
  newRef DESC 
LIMIT
  0, 25;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文