我想在同一個查詢中從多個表中進行計數,並且2個計數的結果是完全錯誤的。我在此查詢中使用以下3個表:從子表中選擇2個不同子表的計數
CREATE TABLE `assignments` (
`id` int(11) NOT NULL,
`lead_id` int(11) NOT NULL,
`buyer_id` int(11) NOT NULL,
`refunded` int(11) NOT NULL DEFAULT '0',
`date_assigned` int(11) NOT NULL,
`date_refunded` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `leads` (
`id` int(11) NOT NULL,
`vertical_id` int(11) NOT NULL,
`source_id` int(11) NOT NULL,
`agent_id` varchar(255) DEFAULT NULL,
`status_id` int(11) NOT NULL DEFAULT '0',
`completeness` enum('Partial','Complete') DEFAULT NULL,
`freshness` enum('New','Duplicate') NOT NULL,
`date_created` int(11) NOT NULL,
`date_updated` int(11) NOT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `verticals` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
我想要做的,就是搶的結果,將它們放入一個像這樣的表:
Name New Duplicate Partial Complete Total Assigned Refunded
Automotive 4 1 3 2 5 36 9
Education 16 7 9 14 23 36 9
第5個數字是正確的。問題是分配和退回的列號是完全錯誤的。這裏是我的查詢:
select v.*,
(select count(*) from `leads` where vertical_id=v.id and `freshness`='New' and `date_created` between 1470009600 and 1471923227) as `new`,
(select count(*) from `leads` where vertical_id=v.id and `freshness`='Duplicate' and `date_created` between 1470009600 and 1471923227) as `duplicate`,
(select count(*) from `leads` where vertical_id=v.id and `completeness`='Partial' and `date_created` between 1470009600 and 1471923227) as `partial`,
(select count(*) from `leads` where vertical_id=v.id and `completeness`='Complete' and `date_created` between 1470009600 and 1471923227) as `complete`,
(select count(*) from `leads` where vertical_id=v.id and `date_created` between 1470009600 and 1471923227) as `total`,
(select count(*) from `assignments` where lead_id=l.id and `refunded`=0) as `assigned`,
(select count(*) from `assignments` where lead_id=l.id and `refunded`=1) as `refunded`
from `verticals` as v
left join `leads` as l on (l.vertical_id = v.id)
where l.date_created between 1470009600 and 1471923227
group by v.id
我該如何糾正這種情況,而不使用子選擇中的子選擇(這對性能來說會很糟糕)?
編輯:我相信,我幾乎有這個工作,但必須有寫這個查詢一個更好的方法(也,它似乎是分組的assignments
通過lead_id
):
select o.*,
(select count(*) from `leads` where {$sql_column}=o.id and `freshness`='New' and `date_created` between {$date_from} and {$date_to}) as `new`,
(select count(*) from `leads` where {$sql_column}=o.id and `freshness`='Duplicate' and `date_created` between {$date_from} and {$date_to}) as `duplicate`,
(select count(*) from `leads` where {$sql_column}=o.id and `completeness`='Partial' and `date_created` between {$date_from} and {$date_to}) as `partial`,
(select count(*) from `leads` where {$sql_column}=o.id and `completeness`='Complete' and `date_created` between {$date_from} and {$date_to}) as `complete`,
(select count(*) from `leads` where {$sql_column}=o.id and `date_created` between {$date_from} and {$date_to}) as `total`,
(select count(*) from `assignments` where `lead_id` in
(select `id` from `leads` where {$sql_column}=o.id and `date_created` between {$date_from} and {$date_to}) and `refunded`=0) as `assigned`,
(select count(*) from `assignments` where `lead_id` in
(select `id` from `leads` where {$sql_column}=o.id and `date_created` between {$date_from} and {$date_to}) and `refunded`=1) as `refunded`
from {$sql_object} as o
可以提供相同的一些記錄? –