我有一张如下表:
+-------------+--------------+
| current_day | browser |
+-------------+--------------+
| 2016-05-02 | Safari |
| 2016-05-03 | Safari |
| 2016-05-04 | Safari |
| 2016-05-05 | Safari |
| 2016-05-06 | Safari |
| 2016-05-07 | Safari |
| 2016-05-08 | Safari |
| 2016-05-09 | Opera |
| 2016-05-10 | Opera |
| 2016-05-11 | Opera |
| 2016-05-12 | Opera |
| 2016-05-13 | Opera |
| 2016-05-14 | Opera |
| 2016-05-15 | Chrome |
| 2016-05-16 | Firefox |
| 2016-05-17 | Firefox |
| 2016-05-18 | Firefox |
| 2016-05-19 | Firefox |
| 2016-05-20 | Firefox |
| 2016-05-21 | Firefox |
| 2016-05-22 | Firefox |
| 2016-05-23 | Safari |
| 2016-05-24 | Safari |
| 2016-05-25 | Safari |
| 2016-05-26 | Safari |
| 2016-05-27 | Safari |
| 2016-05-28 | Safari |
| 2016-05-29 | Safari |
| 2016-05-30 | Opera |
| 2016-05-31 | Opera |
| 2016-06-01 | Opera |
| 2016-06-02 | Firefox |
| 2016-06-03 | Firefox |
| 2016-06-04 | Firefox |
| 2016-06-05 | Firefox |
| 2016-06-06 | Firefox |
| 2016-06-07 | Firefox |
| 2016-06-08 | Firefox |
| 2016-06-09 | Chrome |
| 2016-06-10 | Chrome |
| 2016-06-11 | Chrome |
| 2016-06-12 | Chrome |
| 2016-06-13 | Chrome |
| 2016-06-14 | Chrome |
| 2016-06-15 | Chrome |
| 2016-06-16 | Chrome |
+-------------+--------------+
我想在“浏览器”中提取每个序列的第一个和最后一个序列。列,换句话说,结果如下:
+-------------+--------------+
| current_day | browser |
+-------------+--------------+
| 2016-05-02 | Safari |
| 2016-05-08 | Safari |
| 2016-05-09 | Opera |
| 2016-05-14 | Opera |
| 2016-05-15 | Chrome |
| 2016-05-16 | Firefox |
| 2016-05-22 | Firefox |
| 2016-05-23 | Safari |
| 2016-05-29 | Safari |
| 2016-05-30 | Opera |
| 2016-06-01 | Opera |
| 2016-06-02 | Firefox |
| 2016-06-08 | Firefox |
| 2016-06-09 | Chrome |
| 2016-06-16 | Chrome |
+-------------+--------------+
创建MYSQL查询是否可行/方便?或者更好地提取所有结果并使用php执行一些后期处理?
答案 0 :(得分:1)
您可以使用以下查询:
SELECT MIN(current_day) AS start_day,
MAX(current_day) AS stop_day,
browser
FROM (
SELECT current_day, browser,
@grp := IF(@br = browser, @grp,
IF(@br := browser, @grp+1, @grp+1)) AS grp
FROM mytable
CROSS JOIN (SELECT @grp := 0, @br := '') AS vars
ORDER BY current_day) AS t
GROUP BY browser, grp
上述查询使用变量,以便识别具有相同browser
值的连续记录的孤岛。它为每个浏览器返回一行。
您必须重复相同的子查询两次并使用UNION
,如果您想为每个最小/最大日期获得两个单独的行。
答案 1 :(得分:0)
试试这个:
SELECT current_day, browser
FROM table_name AS a
WHERE current_day = (
SELECT MAX(current_day)
FROM table_name AS b
WHERE a.browser = b.browser
)
OR current_day = (
SELECT MIN(current_day)
FROM table_name AS c
WHERE a.browser = c.browser
)
方法稍微简单:
select min(current_day), max(current_day), browser from test group by browser;
答案 2 :(得分:0)
这里有2个样本,每个浏览器一行和两行
一行
SELECT
min(current_day) AS start_date
, max(current_day) AS end_date
, browser
FROM your_table
GROUP BY browser;
两行
SELECT * FROM (
SELECT
min(current_day)
, browser
FROM your_table
UNION ALL
SELECT
max(current_day)
, browser
FROM your_table
)
ORDER BY broser,current_day;
答案 3 :(得分:0)
试试这个
SELECT r.* FROM (SELECT t.* FROM (SELECT k.* FROM (SELECT * FROM `tbl_test` ORDER BY `current_day` DESC) as k GROUP BY k.browser) as t UNION SELECT h.* FROM (SELECT q.* FROM (SELECT * FROM `tbl_test` ORDER BY `current_day` ASC) as q GROUP BY k.browser) as h ) as r ORDER BY r.current_day