【问题标题】:Mysql: Get rows with unique column per pageMysql:获取每页具有唯一列的行
【发布时间】:2011-06-15 03:43:16
【问题描述】:

开始执行我的英语,但我需要一个 sql 的帮助:

我有一个包含优惠的表格,例如:ID、id_user、offer、timestamp

而且我需要得到一个带排序的行,这将是页面上的一个 id_user(例如,每 10 行中只有一个唯一的 id_user,第二个带有时间戳的订单)


例子:

1, Pepa, Auto, 1.1.2011
2, Pepa, Motorka, 1.1.2011
3, Karel, Traktor, 2.1.2011
4, Lukas, Jeep, 2.1.2011
5, Pepa, Autokara, 3.1.2011
6, Jindra, Traktor, 5.1.2011

=> 排序(页面上有 2 行)

**1. Page**
1, Pepa, Auto, 1.1.2011
3, Karel, Traktor, 2.1.2011


**2. Page**
2, Pepa, Motorka, 1.1.2011
4, Lukas, Jeep, 2.1.2011


**3. Page**
5, Pepa, Autokara, 3.1.2011
6, Jindra, Traktor, 5.1.2011

在简单的“一页上的唯一用户优惠”中

感谢您的帮助!

【问题讨论】:

  • 你能写一个伪代码算法来解释你如何决定哪些行应该放在哪里吗?在计算上可能很难找到一种方法来排列数据,以便没有用户在同一页面上出现两次。这就像背包问题,是NP-Complete。
  • 行数不多的情况需要处理吗?例如,在您的示例中,如果您想在页面上显示 5 行,它将如何工作?即使总共只有 6 个条目,是否仍然有 3 页,因为有 3 行 Pepa 行?

标签: php mysql sql


【解决方案1】:

你试过“分组”吗?

delimiter //

connect pepa

drop table if exists offers;
create table offers (
id SERIAL,
id_user VARCHAR(20) NOT NULL,
offer VARCHAR(20) NOT NULL,
timestamp TIMESTAMP DEFAULT NOW()
);

insert into offers
(id_user,offer)
values
('Pepa', 'Auto'),
('Pepa', 'Motorka'),
('Karel', 'Traktor'),
('Lukas', 'Jeep'),
('Pepa', 'Autokara'),
('Jindra', 'Traktor');

select * from offers group by id_user order by timestamp;

//

这会产生:

id  id_user offer   timestamp
4   Lukas   Jeep    2011-06-05 21:14:10
6   Jindra  Traktor 2011-06-05 21:14:10
1   Pepa    Auto    2011-06-05 21:14:10
3   Karel   Traktor 2011-06-05 21:14:10

请注意,没有一个 id_users 是重复的。如果您包含条件语句(“where”),您可能可以根据登录人的 id_user 创建一个唯一页面。

希望这会有所帮助。 :)

干杯!

(请注意,这里按时间戳排序可能有点奇怪。不要忘记您始终可以使用您选择的语言 [PHP 等] 发布流程。)

【讨论】:

  • 此查询使用 GROUP BY 但只返回唯一行,但我需要具有特定 ORDER 的所有行(每页仅唯一行)。
【解决方案2】:

此 PHP 代码将产生与您在问题中列出的相同的输出。它可能不是世界上最有效的东西,但它可以完成工作。

您也许可以编写一个时髦的 MySQL 查询来执行此操作,但我不知道它在扩展数千条记录等方面的效果如何。而且您正在生成页面。 :)

<?php

// Collect stuff from the database
$dbc=mysqli_connect('127.0.0.1','user','passwd','pepa') or 
 die('Could not connect!');
$getOffers='select * from offers';
$rs=mysqli_query($dbc,$getOffers);
while($thisRow=mysqli_fetch_assoc($rs))
 $offers[]=$thisRow;
mysqli_close($dbc);

// Create the pages
// (this is probably a bit over the top, but you get the idea)
foreach($offers as $oI => $thisOffer)
 $offers[$oI]['used']=false;  // <-- tell us if we've used the record or not
$thisUser='Pepa';  // <-- the user who should appear at the top of each page
$numRecsPerPage=2; // <-- the number of records per page
$cPg=-1; foreach($offers as $oI => $thisOffer) {
    if($thisOffer['id_user']==$thisUser) {
        $cPg++;
        $offers[$oI]['used']=true;
        $page[$cPg][]=$thisOffer;
        $recsUsed=1; foreach($offers as $pI => $procOffer) {
            if(!$offers[$pI]['used'] && $offers[$pI]['id_user']!=$thisUser) {
                $offers[$pI]['used']=true;
                $page[$cPg][]=$procOffer;
                $recsUsed++;
            }
            if ($recsUsed>=$numRecsPerPage) break;
        }
    }
}  

// Print the pages
foreach($page as $thisPage) { 
    foreach($thisPage as $thisRow)
     echo   $thisRow['id']."\t".$thisRow['id_user']."\t".
            $thisRow['offer']."\t".$thisRow['timestamp']."\n";
    echo "\n";
}

?>

输出:

1   Pepa    Auto    2011-06-05 21:14:10
3   Karel   Traktor 2011-06-05 21:14:10

2   Pepa    Motorka 2011-06-05 21:14:10
4   Lukas   Jeep    2011-06-05 21:14:10

5   Pepa    Autokara    2011-06-05 21:14:10
6   Jindra  Traktor 2011-06-05 21:14:10

很抱歉添加另一个答案 - 否则我会添加评论,但我认为代码在这里更有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多