-2

我有一张这样的桌子:

----------
id |  name |  tel  |  email
----------
1    john    0241    re@yah
2            0534    re@rra
3            435     fd@rar
4    geo     43435   re@eae
5            2347    ui@ear
6            678     re@yaya

我想进行这样的查询:

SELECT tel, email FROM table  WHERE name='geo'

和输出是这样的:

----------
  tel  |  email

----------

43435   re@eae
2347    ui@ear
678     re@yaya 

非常感谢您提前

4

5 回答 5

2
SELECT tel, email
FROM table1
JOIN (SELECT MAX(startid) startid,
            IFNULL(MAX(endid), (SELECT MAX(id) FROM table1)) endid
     FROM (SELECT id startid, NULL endid FROM table1 WHERE name = 'geo'
           UNION
           SELECT NULL, MIN(id)-1 FROM table1
           WHERE name != ''
           AND id > (SELECT id FROM table1 WHERE name = 'geo')) x) y
ON id BETWEEN startid AND endid

SQLFIDDLE

于 2013-07-02T11:11:40.043 回答
1
SELECT tel, name FROM
(SELECT id FROM table WHERE name = 'geo') a
LEFT JOIN table b ON
b.id = a.id
WHERE b.id >= a.id AND b.id < (SELECT id from table WHERE name > '' 
ORDER BY ID ASC
LIMIT 1) c
于 2013-07-02T11:00:09.833 回答
1

使用用户变量:-

SELECT tel, email
FROM
(
    SELECT Sub1.id, Sub1.name, Sub1.tel, Sub1.email, @name:=IF(name IS NULL, @name, name) AS aName
    FROM (SELECT * FROM geoname ORDER BY id) Sub1
    CROSS JOIN (SELECT @name:="") Sub2
) Sub3
WHERE aName = 'geo'

正如 Barmar 强调的那样,这仅在名称字段为空时为 NULL 而不是 '' 时才有效。如果它需要处理空白和 NULL:-

SELECT tel, email
FROM
(
    SELECT Sub1.id, Sub1.name, Sub1.tel, Sub1.email, @name:=IF(name IS NULL OR name = "", @name, name) AS aName
    FROM (SELECT * FROM Table1 ORDER BY id) Sub1
    CROSS JOIN (SELECT @name:="") Sub2
) Sub3
WHERE aName = 'geo'

玩的有点多。非常喜欢 Barmar 的解决方案,但我想看看我是否可以简化和删除几个子选择:-

SELECT a.*
FROM geoname a
INNER JOIN
(
    SELECT a.id AS MinId, IFNULL(MIN(b.id), MAX(c.id)) AS MaxId
    FROM geoname a
    LEFT OUTER JOIN geoname b
    ON a.id < b.id AND b.name IS NOT NULL
    CROSS JOIN geoname c
    WHERE a.name = 'geo'
    GROUP BY a.id
) Sub1
ON a.id BETWEEN Sub1.MinId AND Sub1.MaxId

似乎稍微快一点,解释也更简单。

或者处理空白以及 NULLS

SELECT a.*
FROM geoname a
INNER JOIN
(
    SELECT a.id AS MinId, IFNULL(MIN(b.id), MAX(c.id)) AS MaxId
    FROM geoname a
    LEFT OUTER JOIN geoname b
    ON a.id < b.id AND b.name IS NOT NULL OR b.name = ''
    CROSS JOIN geoname c
    WHERE a.name = 'geo'
    GROUP BY a.id
) Sub1
ON a.id BETWEEN Sub1.MinId AND Sub1.MaxId
于 2013-07-02T11:18:48.017 回答
0
SELECT i.id, i.tel, i.email
FROM 
    tableX AS t
  JOIN
    tableX AS i
      ON  i.id >= t.id
      AND i.id < COALESCE(
          ( SELECT n.id
            FROM tableX AS n
            WHERE n.id > t.id
              AND n.name <> ''
            ORDER BY n.id ASC 
              LIMIT 1
          ), 2147483647)
WHERE
    t.name = 'geo' ;

SQL-Fiddle-1测试(thnx @Barmar)

于 2013-07-02T11:12:42.887 回答
0

假设 id 正在增加并且没有名称的下一个条目属于同一个条目,那么相应地更新您的表将是最容易的。

就是这样:

Test data:
/*
drop table yourTable;
CREATE TABLE yourTable
    (`id` int, `name` varchar(40), `tel` int, `email` varchar(20))
;

INSERT INTO yourTable
    (`id`, `name`, `tel`, `email`)
VALUES
    (1, 'john', 0241, 're@yah'),
    (2, NULL, 0534, 're@rra'),
    (3, NULL, 435, 'fd@rar'),
    (4, 'geo', 43435, 're@eae'),
    (5, NULL, 2347, 'ui@ear'),
    (6, NULL, 678, 're@yaya'),
     (7, 'anything', 789, 'whatever')
;
#*/


UPDATE yourTable yt1 INNER JOIN (
SELECT
yt.*,
COALESCE(name, @prev) AS newName,
CASE WHEN name IS NOT NULL THEN @prev:=name END
FROM
yourTable yt
, (SELECT @prev:=NULL) v
ORDER BY id
) yt2 ON yt1.id = yt2.id
SET yt1.name = yt2.newName;

SELECT * FROM yourTable;

结果:

id    name       tel    email
1    john        241    re@yah
2    john        534    re@rra
3    john        435    fd@rar
4    geo         43435  re@eae
5    geo         2347   ui@ear
6    geo         678    re@yaya
7    anything    789    whatever

那么您的查询应该可以正常工作。

于 2013-07-02T11:12:01.950 回答