使用多个表的优化方式查询结果

时间:2016-12-29 08:52:29

标签: php mysql sql

我正在尝试使用4个表获取数据。 表格列表

  • 呼叫
  • ref_label_no
  • 标签

我想获取没有使用特定标签附加的来电号码的数据。 这是表。
数字表

select id,uuid,number from numbers limit 1;
+----+--------------------------------------+------------+
| id | uuid                                 | number     |
+----+--------------------------------------+------------+
|  1 | ed268b05-758e-44fd-b429-8d5223651814 | 1234561222 |
+----+--------------------------------------+------------+

致电表

mysql> select id,uuid,did from calls limit 2;
+----+-------------------------------------+------------+
| id | uuid                                | number     |
+----+-------------------------------------+------------+
|  1 | ddddass-b810-4f23-7456-8ff56efab080 | 1234561222 |
|  2 | dddddd-b810-4f23-7456-8ff56efab080  | 123456789  |
+----+-------------------------------------+------------+
2 rows in set (0.02 sec)

ref_label_no表

mysql> select uuid,number_uuid,label_uuid from ref_label_no limit 1;
+--------------------------------------+--------------------------------------+--------------------------------------+
| uuid                                 | number_uuid                          | label_uuid                           |
+--------------------------------------+--------------------------------------+--------------------------------------+
| 7a5e1b6e-0194-4993-8d78-5f7a1b60c7d2 | ed268b05-758e-44fd-b429-8d5223651814 | 7262b06e-9263-4825-8411-4c107104a60b |
+--------------------------------------+--------------------------------------+--------------------------------------+
1 row in set (0.00 sec)

标签表

mysql> select uuid,name from labels limit 1;
+--------------------------------------+-------+
| uuid                                 | name  |
+--------------------------------------+-------+
| 7262b06e-9263-4825-8411-4c107104a60b | INDIA |
+--------------------------------------+-------+
1 row in set (0.00 sec)

现在我希望结果如呼叫号码与总呼叫。[此输出我需要]

mysql> select uuid,name from labels limit 1;
+--------------------------------------+-------+
| Number                       | Total_calls  |
+--------------------------------------+-------+
| 13456789                     |       2      |
+--------------------------------------+-------+
1 row in set (0.00 sec)

我已经进行了以下查询,但没有成功也花费太多时间来回复。

select count(*) as Total_calls,calls.number as did,labels.name,calls.created_at as callstart from calls, labels JOIN ref_label_no ON labels.uuid = ref_label_no.label_uuid JOIN numbers ON numbers.uuid = ref_label_no.number_uuid where labels.name="INDIA"  group by calls.number;

任何建议或建议?

2 个答案:

答案 0 :(得分:0)

多个表格避免使用加入,请使用联盟。它会比加入更快。

像这样,

select uuid,did from calls union 
select uuid,number_uuid as did from ref_label_no union
select uuid,name as did from labels

答案 1 :(得分:0)

SELECT COUNT(calls.uuid) as total_calls, numbers.number
FROM numbers JOIN calls ON numbers.number = calls.number
JOIN ref_label_no ON ref_label_no.number_uuid = numbers.uuid
JOIN labels ON labels.uuid = ref_label_no.label_uuid
WHERE labels.name="INDIA"
GROUP BY numbers.number;

由于我们正在计算按编号分组的呼叫总数,因此您无法在查询结果中选择calls.created_at,因为它与聚合函数COUNT冲突。

以下查询使用EXISTS条件:

SELECT COUNT(calls.uuid) as total_calls, numbers.number
FROM calls JOIN numbers ON numbers.number = calls.number
WHERE EXISTS(SELECT 1 FROM ref_label_no JOIN labels ON labels.uuid = ref_label_no.label_uuid
WHERE labels.name="INDIA" AND numbers.uuid = ref_label_no.number_uuid)
GROUP BY numbers.number;