【问题标题】:Combine 2 queries mysql and organise results 5:1 insertion合并2个查询mysql并组织结果5:1插入
【发布时间】:2022-01-19 04:45:44
【问题描述】:

我被一个很可能很愚蠢的问题弄得不知所措。

我正在使用 php + mysql 在我的网站上投放广告。

我正在考虑对广告使用与普通帖子相同的表格,因此,有 7 种不同类型的广告,每 5 个普通广告类型组合 1 个“赞助”广告类型。

 CREATE TABLE `posts` (
 `post_id` int(11) NOT NULL,
 `type` enum('type1','type2','type3','type4','type5','type6','type7','sponsored') CHARACTER SET utf16 NOT NULL DEFAULT 'type1',
 
 PRIMARY KEY (`post_id`),
 ) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_unicode_ci

理想的结果应该是:

广告类型 1-类型 7:1,2,3,4,5 广告赞助 (1) 广告类型1-类型7:6,7,8,9,10 广告赞助 (2) 广告类型1-类型7:11、12、13、14、15 并继续重复模式以获得结果,如果没有更多赞助商,则插入默认广告。

如果没有找到与查询参数相关的赞助广告:

1,2,3,4,5-(默认)-6,7,8,9,10-(默认)-11,12,13,14,15-(默认)...

现在我正在使用一个 php 文件,每 5 个“正常”广告插入一个默认赞助广告。

样本数据

post_id  type    location
1        type2    Bahamas
2        type4    Seville
4       sponsored   Bahamas
5       sponsored   Singapur
6       type1     Bilbao
7      sponsored   Bahamas
8     type1       Bahamas
9     type6       Bahamas
10     type4       Bahamas
11     type1       New York
12     type1       Bahamas
13     type6       Bahamas

在索引处显示的输出 样本数据

post_id  type    location
1        type2    Bahamas
2        type4    Seville
6       type1     Bilbao
8      type1       Bahamas
9      type6       Bahamas
xx     sponsored   xxxx    >> RANDOM SPONSOR
10     type4       Bahamas
11     type1       New York
12     type1       Bahamas
13     type6       Bahamas

定义位置“巴哈马”时需要输出样本

post_id  type    location
1    type2       Bahamas
8     type1       Bahamas
9     type6       Bahamas
10     type4       Bahamas
12     type1       Bahamas
2     sponsored Bahamas
13     type6       Bahamas

现在我正在执行 2 个基本的 SELECT 查询(1 个用于普通广告,1 个用于赞助广告)并将结果存储在我组合的数组中。

使用 php,我每 5 个普通广告插入 1 个广告。

我的目标是优化我的做法,因为我不知道是否有任何 mysql 参数可以改进我的公式。

是的,ADyson 听起来像一个插入,因为用户像普通广告一样插入赞助广告,这就是我试图用同一张表组织所有内容的原因。否则我可以为广告创建另一个表,但只要我不太了解 mysql,就看不到区别。我迷失了 JOIN、INNER JOIN、INTERSECT、IF... 我在这里浏览了不同的帖子,但看不到任何与此问题匹配的内容。

也许我在寻找类似Slicing a Mysql array 的东西,不确定。

提前致谢-

【问题讨论】:

  • 问题不清楚。提供:精确的 MySQL 版本信息、源表的 CREATE TABLE、一些示例数据作为 INSERT INTO、该数据的所需输出以及每个输出行的详细说明。
  • 您希望我们解决您的问题,但不自己解决?这是你的选择......
  • 我也不太明白这个问题。与如何插入赞助广告有关?目前还不清楚究竟是什么让你在这项任务中遇到了困难。请编辑问题
  • 为什么不只是 SELECT * 没有 WHERE?然后他们都会被选中。当然,您可能需要一些东西来订购它们。如果有的话,我们看不到它们当前在表中的排序方式。一些示例源数据会很棒。这将有助于向我们提供您的问题的minimal reproducible example
  • 顺便说一句,这个问题听起来像是关于将数据插入到帖子表中,而不是选择它进行输出(大概是插入到数组中?)。 这就是您应该显示完整代码的原因(请在问题中,而不是在 cmets 中)并准确解释发生了什么。 这就是人们发现事情不清楚的原因。我们看不到你的机器,也不是读心者,我们只能使用我们面前的东西,这不是很多。另见How to Ask

标签: php mysql arrays insert ads


【解决方案1】:

每 5 个帖子我将插入 1 个广告。

$counter = 0;
foreach($posts as $post) { 
if(++$counter % 5 === 1) { 
 include './advertisment/advertisment.php';
    }

现在在我的广告查询之上:

  $limit = intval($counter);
  $offset = intval($counter/5);

在我的情况下,如果传递了参数位置,则广告查询会按位置查找广告,如果没有传递位置,则检索任何广告,其余插入使用默认广告。我的赞助商广告有效期为 3 个月,这就是为什么我还要检查间隔。

if ((isset($_GET['location'])) && (!empty($_GET['location']))) {                
                $location = $_GET['location'];
                $query = $pdo->prepare("SELECT * FROM `posts`
            WHERE type = 'advertisment' AND DATE(post_on) > NOW() - INTERVAL 12 WEEK 
            AND id IN (SELECT id FROM `postcontenttable` WHERE location = :location AND posts.id = postcontenttable.post_id)   
            ORDER BY RAND() LIMIT :limit OFFSET :offset");
                $query->bindParam(":location" , $location , PDO::PARAM_STR);                                
            } else {    
                $query = $pdo->prepare("SELECT * FROM `posts`
            WHERE type = 'advertisment' AND DATE(post_on) > NOW() - INTERVAL 12 WEEK   
            ORDER BY RAND() LIMIT :limit OFFSET :offset");              
            }
            $query->bindParam(':limit', $limit, PDO::PARAM_INT);
            $query->bindParam(':offset', $offset, PDO::PARAM_INT);
            $query->execute();
            $ads = $query->fetchAll(PDO::FETCH_OBJ);
if (!($query->rowCount() == 0)) { 

foreach($ads as $post) { 
if(++$offset % 1 === 0) { 
    break;
} 
}
} else { ?>
<?php include './advertisment/ad.php'; ?>
<?php } ?>

因此,我每 5 个正常帖子插入 1 个广告,将赞助商展示在他们偏好的位置,并在不可用时插入默认广告(您可以使用它来检索随机赞助商)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多