gpt4 book ai didi

php - MySQL查询有问题

转载 作者:行者123 更新时间:2023-11-29 14:10:15 25 4
gpt4 key购买 nike

我正在使用 PHP/MySQL,我必须从现有表创建一个新表。这是有问题的:

表1:

photo_id   email          name_to   creation_date    link
1 foo1@bar.com paul 2012-11-21 link1.com
2 foo2@bar.com mark 2012-11-22 link2.com
3 foo1@bar.com alex 2012-11-23 link3.com
4 foo1@bar.com saul 2012-11-25 link4.com
5 foo1@bar.com john 2012-11-26 link5.com
6 foo2@bar.com math 2012-11-27 link6.com
7 foo3@bar.com fred 2012-11-28 link7.com

表1中,电子邮件不是唯一的,可以重复多次。每个链接都不同。有了这些数据,我必须创建一个新表,其中电子邮件是唯一的,如果一封电子邮件有更多条目,则最多有 3 个链接(如果是这样,我需要 3 个最新条目的数据)。

因此,在这种情况下,Table2 将类似于:

email           link1        creation_date1    name_to1    link2        creation_date2    name_to2    link3        creation_date3    name_to3
foo1@bar.com link5.com 2012-11-26 john link4.com 2012-11-25 saul link3.com 2012-11-23 alex
foo2@bar.com link6.com 2012-11-27 math link2.com 2012-11-22 mark
foo3@bar.com link7.com 2012-11-28 fred

我知道 GROUP_CONCAT 功能,但这并不是我真正需要的,因为链接都在同一列中。是否最好进行 SELECT * FROM table1 并将结果处理到 PHP 数组中,然后创建 Table2 或唯一的 MySQL 查询就可以解决问题?或者创建多个MySQL表?

Table1 超过 1000 万行。

如有任何建议,我们将不胜感激。

谢谢。

最佳答案

1) 选择所有唯一的电子邮件。

2) 对于每封电子邮件,取该电子邮件的前 3 行,按创建日期降序排列。

3)使用该数据插入到新表中。

你觉得怎么样?

关于php - MySQL查询有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13611718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com