我搜索过但无法找到可以成功适应我的查询的解决方案。
我已经建立了一个包含广告横幅的数据库,我的脚本会旋转并连续显示每个横幅,但是我试图在查询中添加将在表格中搜索并显示点击率最高的横幅(点击通过比率)基于其MIN展示次数(展示横幅的次数)和MAX点击次数(点击横幅的次数)
我尝试过搜索查询的许多不同组合,其中一些语法错误,而其他语法似乎具有正确的语法但未能产生预期结果,这是我尝试的最后一个查询:
$query = "SELECT banner_id,banner_url,banner_alt,banner_size FROM banner_ads WHERE $banner_niche$banner_size banner_enable=1 AND banner_impressions=(SELECT MIN(banner_impressions) FROM banner_ads WHERE banner_clicks=(SELECT MAX(banner_clicks) FROM banner_ads));";
$result = mysql_query($query) or die('Query failed: ' . mysql_error() . "\nQuery: $query");
$row = mysql_fetch_array($result, MYSQL_NUM);
$banner_id = $row[0];
$banner_url = $row[1];
$banner_alt = $row[2];
list($banner_width, $banner_height) = split("x", $row[3]);
字段内容的示例是:
banner_id / banners row ID number : 1
banner_url / URL to the banner image : http://somesite.com/banner.jpg
banner_alt / banner alt text : Visit SomeSite.com
banner_size / size of the banner : 468x60
banner_niche / the category of the banner : books
banner_enable / is the banner set to display or not : 1
banner_impressions / number of times banner has been shown : 1000
banner_clicks / number if time banner has been clicked : 50
因此,我只需查询即可在表格中搜索点击次数最少的横幅广告。
----------------------------------------------
banner_id | banner_impressions | banner_clicks
----------------------------------------------
1 | 1000 | 50
2 | 100 | 80
3 | 2000 | 90
----------------------------------------------
因此在上面的示例中,banner_id 2将符合条件。 (我跳过了示例中的其他字段,因为它们不相关)
先谢谢你们,我希望我已经很好地解释了我想要实现的目标。
答案 0 :(得分:0)
要获得点击次数与横幅广告的最高比率,此查询将起作用
select banner_id, banner_impressions, banner_clicks, rank() over
(order by click_toimpression DESC)
as ranks FROM
(
select banner_id, banner_impressions, banner_clicks,
(banner_clicks / banner impressions)
as Click_ToImpression FROM banner_ads
);
- 获得最高排名
Select * FROM
(
select banner_id, banner_impressions, banner_clicks, rank() over
(order by click_toimpression DESC)
as ranks FROM
(
select banner_id, banner_impressions, banner_clicks,
(banner_clicks / banner impressions)
as Click_ToImpression FROM banner_ads
)) WHERE ranks = 1
MySQL
select banner_id, banner_impressions, banner_clicks,
(banner_clicks / banner impressions)
as Click_ToImpression FROM banner_ads) ORDER BY Click_toImpression DESC
LIMIT 1;
这将对您的比率进行排名。要获得最高,只需从中选择最大值,如果您只想要一行。
按照点击横幅广告比例对其进行排名,而不是随意获取最小值和最大值,并尝试从中收集信息。我认为这与您正在寻找的内容一致,以及我将如何解决这个问题。