此查询无法使用此表,那么如何使用此表更新查询此fornula?

时间:2017-12-02 09:18:16

标签: php mysql mysqli phpmyadmin

表AAA

SELECT p1.keyword as keyword, 
SUM(
SELECT  sum(`click`) * price  FROM `AAA` p2 where main_id = '1' AND p2.keyword =  p1.keyword  AND DATE(p2.click_time) BETWEEN '2017-11-01' AND '2017-11-30' GROUP BY p2.price ORDER BY NULL
)                                       
as new1
FROM AAA p1
where p1.main_id = '1' AND DATE(p1.click_time) BETWEEN '2017-11-01' AND  '2017-11-30' 
GROUP BY p1.keyword
ORDER BY NULL LIMIT 0,20

我的查询

click

以上查询检查,以便获得此类错误

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'SELECT sum(AAA)* cpc FROM keyword sum(click) price aaa 4 0.25 0.36 0.25 0.25 ( 0.36 * 1 + 0.25 * 3 ) bbb 4 0.36 0.45 0.55 0.45 ( 0.36 * 1 + 0.45 * 2 + 0.55 * 1 ) ccc 2 0.98 0.98 ( 0.98 * 2 ) original & final result getting this types keyword price aaa 0.86 bbb 1.81 ccc 1.96 p2附近使用正确的语法,其中main_id ='1'和p2.ke'在第3行

此查询是否正确,但此查询也进行了另一个引用.. !!

最终结果

dict = {
'columns': ['one', 'two', 'three'],
'data': [
['header1', '111', '222', '333'],
['header2', '111', '222', '333'],
['header3', '111', '222', '333'],
['header4', '111', '222', '333']
]}

1 个答案:

答案 0 :(得分:1)

我更改了查询并按如下方式进行了更改:

SELECT keyword,SUM(aa.eachSum) AS totalSum, SUM(aa.totalClicks) AS totalClicks FROM(
SELECT  GROUP_CONCAT(DISTINCT p2.keyword) AS keyword,COUNT(p2.click) *  price AS eachSum, COUNT(p2.click) AS totalClicks
FROM `values_test` p2 WHERE main_id = '1'  AND DATE(p2.click_time) BETWEEN '2017-11-01' AND '2017-11-30' GROUP BY p2.price,p2.keyword
)aa GROUP BY aa.keyword

<强>解释

SELECT  GROUP_CONCAT(DISTINCT p2.keyword) AS keyword,COUNT(p2.click) *  price AS eachSum, COUNT(p2.click) AS totalClicks
FROM `values_test` p2 
WHERE main_id = '1'  
AND DATE(p2.click_time) BETWEEN '2017-11-01' AND '2017-11-30' 
GROUP BY p2.price,p2.keyword

此内部子查询根据价格和关键字收集不同的关键字,因为当您仅按价格进行分组时,会在一列中收集具有相同价格的不同关键字,我猜这是不需要的。该块结果如下:

keyword eachSum totalClicks
aaa      0.75     3
aaa      0.36     1
bbb      0.36     1
bbb      0.90     2
bbb      0.55     1
ccc      1.96     2

收集thsi数据后,外部查询将对每个关键字进行分组并对每个值进行求和,输出结果如下:

keyword totalSum    totalClicks
aaa      1.11           4
bbb      1.81           4
ccc      1.96           2

你的总和例子中有错误。 如果这是你想要的,请告诉我。 (注意:我根据使用情况更改了表名)