gpt4 book ai didi

php - 如何在 PHP 中以随机顺序从现有 mysql 数据库创建两个新列表,其中相同位置的元素不相等?

转载 作者:行者123 更新时间:2023-11-30 23:26:01 24 4
gpt4 key购买 nike

好的。让我们更清楚一点。

我想做的是:

我有一张这样的 table ...

    +---------+--------------------+--------------------------+    |    id   |    name            | email                    |    +---------+--------------------+--------------------------+    | 1       |  karl              | karl@email.com           |    +---------+--------------------+--------------------------+    | 2       |  fred              | fred@email.com           |    +---------+--------------------+--------------------------+    | 3       |  christopher       | chris@email.com          |    +---------+--------------------+--------------------------+ 

我最终想要的是一个如下所示的新表:

    +-----+---------------+---------------------+--------------+--------------------+    | id  | name1         | email1              | name2        | email2             |    +-----+---------------+---------------------+--------------+--------------------+    | 1   | fred          | fred@email.com      | christopher  | chris@email.com    |    +-----+---------------+---------------------+--------------+--------------------+    | 2   | christopher   | chris@email.com     | karl         | karl@email.com     |    +-----+---------------+---------------------+--------------+--------------------+    | 3   | karl          | karl@email.com      | fred         | fred@email.com     |    +-----+---------------+---------------------+--------------+--------------------+ 

值在水平方向 (id[1].name1 != id[1].name2) 和垂直方向 (id[1].name1 != id[2].name1) 是唯一的。

因此,这个输出应该是不可能的:

    +-----+---------------+---------------------+--------------+--------------------+    | id  | name1         | email1              | name2        | email2             |    +-----+---------------+---------------------+--------------+--------------------+    | 1   | fred          | fred@email.com      | christopher  | chris@email.com    |    +-----+---------------+---------------------+--------------+--------------------+    | 2   | fred          | fred@email.com      | karl         | karl@email.com     |    +-----+---------------+---------------------+--------------+--------------------+    | 3   | karl          | karl@email.com      | karl         | karl@email.com     |    +-----+---------------+---------------------+--------------+--------------------+ 

我怎样才能以简单的方式实现这一点?

最佳答案

我认为这个查询应该可以完成工作:

SELECT id1.name AS name1, id1.email AS email1, id2.name AS name2, id2.email AS email2
FROM table AS id1
JOIN table AS id2 ON ( name1 != name2 )
WHERE name1 < name2

table 是您的表的名称。希望对您有所帮助。

关于php - 如何在 PHP 中以随机顺序从现有 mysql 数据库创建两个新列表,其中相同位置的元素不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13286782/

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