【发布时间】:2015-02-25 00:12:26
【问题描述】:
我想从一个表中进行选择,然后为每个表进行选择。
表格:
category
+====+=======+
| id | title |
+====+=======+
this table has list of category
email
+====+=======+==========+=============+
| id | eMail | domainId | elseColumns |
+====+=======+==========+=============+
this table has list of emails but domains are in another table
domain
+====+========+=============+
| id | domain | elseColumns |
+====+========+=============+
list of domains which used in email
subscriber_category
+========+============+
| userId | categoryId |
+========+============+
list of emails in categories
现在的问题是我如何以最少的运行时间列出类别和其中的电子邮件数量?我的尝试是等待 20 秒等待 200000 封电子邮件和 20 个类别。
sql:
SELECT category.*,
(SELECT COUNT(DISTINCT subscriber_category.userId) FROM subscriber_category
JOIN email ON email.id=subscriber_category.userId
JOIN domain ON domain.id=email.domainId
WHERE subscriber_category.categoryId=category.id
AND email.blackList=0
AND domain.blackList=0
) AS qty
FROM category WHERE category.userId=1 ORDER BY category.title ASC
【问题讨论】:
标签: php mysql sql normalization database-normalization