mysql - 寻找在 Mysql 中查询多个电子邮件地址的最有效方法

标签 mysql sql

我正在寻找一种方法来选择 joe 的电子邮件地址,以便每次运行选择查询时,我只得到一个按 ID 顺序返回的电子邮件地址。对结果进行排序..

      so on first select I get joe@blogs.com 
      and then second I get joe@gmail.com
      and third joe@outlook.com
      and next joe@blogs.com as there are only three entries

如果你明白我的意思。

            addresses
            ---------------------
            id|name|email
            ---------------------
            1 |joe |joe@blogs.com
            2 |joe |joe@gmail.com
            3 |joe |joe@outlook.com

最佳答案

试一下:

CREATE TABLE #results (id INT, name VARCHAR(50), email VARCHAR(100))
DECLARE @currentID INT;

WHILE(1 = 1)
BEGIN
    SET @currentID =
    (SELECT TOP 1 t.id 
    FROM [YOURTABLE] t
    LEFT JOIN #results r
        ON r.id = t.id
    WHERE t.name = 'joe'
        AND r.id IS NULL)

    IF @currentID IS NULL
        BREAK;

    SELECT * FROM [YOURTABLE] WHERE id = @currentID 

    INSERT INTO #results
    SELECT TOP 1 t.id, t.name, t.email 
    FROM [YOURTABLE] t
    WHERE t.id = @currentID 
END

关于mysql - 寻找在 Mysql 中查询多个电子邮件地址的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19983760/

相关文章:

php - 显示 mysql 表中的特定数据

mysql - 创建与 SQL 中的条目排名位置相对应的列

c# - 合理的 SQL 超时

mysql - 返回 SELECT 匹配,其中两行与不同的列值匹配

python - contains_eager 和 joinedload 之间的 SQLAlchemy 区别

sql - 将 SQL 表转换为以列为父节点的 XML

php - 如何在 WordPress 中正确过滤掉具有独特类别的自定义帖子类型?

php - 有没有办法获取最后插入行的日期?

mysql - SQL 查询问题 : Cannot get desired output

Python - 将列表作为参数传递给 SQL,以及更多变量