MySQL « сложная выборка данных отсеивая повторы

Здравствуйте.
Есть таблица 'tableName':
------------------------------
| dt | tm | ip |
| 2012-11-01 | 13-02-03 | 192.168.0.1 |
| 2012-11-01 | 13-05-34 | 192.168.0.2 |
| 2012-11-01 | 14-08-23 | 192.168.0.1 |
| 2012-11-02 | 17-01-22 | 192.168.0.2 |
| 2012-11-02 | 17-02-45 | 192.168.0.1 |
------------------------------
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .mysql.geshi_code {font-family:monospace;} .mysql.geshi_code .imp {font-weight: bold; color: red;} .mysql.geshi_code .kw1 {color: #990099; font-weight: bold;} .mysql.geshi_code .kw2 {color: #990099; font-weight: bold;} .mysql.geshi_code .kw3 {color: #9900FF; font-weight: bold;} .mysql.geshi_code .kw4 {color: #999900; font-weight: bold;} .mysql.geshi_code .kw5 {color: #999900; font-weight: bold;} .mysql.geshi_code .kw6 {color: #FF9900; font-weight: bold;} .mysql.geshi_code .kw7 {color: #FF9900; font-weight: bold;} .mysql.geshi_code .kw8 {color: #9900FF; font-weight: bold;} .mysql.geshi_code .kw9 {color: #9900FF; font-weight: bold;} .mysql.geshi_code .kw10 {color: #CC0099; font-weight: bold;} .mysql.geshi_code .kw11 {color: #CC0099; font-weight: bold;} .mysql.geshi_code .kw12 {color: #009900;} .mysql.geshi_code .kw13 {color: #000099;} .mysql.geshi_code .kw14 {color: #000099;} .mysql.geshi_code .kw15 {color: #000099;} .mysql.geshi_code .kw16 {color: #000099;} .mysql.geshi_code .kw17 {color: #000099;} .mysql.geshi_code .kw18 {color: #000099;} .mysql.geshi_code .kw19 {color: #000099;} .mysql.geshi_code .kw20 {color: #000099;} .mysql.geshi_code .kw21 {color: #000099;} .mysql.geshi_code .kw22 {color: #000099;} .mysql.geshi_code .kw23 {color: #000099;} .mysql.geshi_code .kw24 {color: #000099;} .mysql.geshi_code .kw25 {color: #000099;} .mysql.geshi_code .kw26 {color: #000099;} .mysql.geshi_code .kw27 {color: #00CC00;} .mysql.geshi_code .coMULTI {color: #808000; font-style: italic;} .mysql.geshi_code .co1 {color: #808080; font-style: italic;} .mysql.geshi_code .co2 {color: #808080; font-style: italic;} .mysql.geshi_code .es0 {color: #004000; font-weight: bold;} .mysql.geshi_code .es1 {color: #008080; font-weight: bold;} .mysql.geshi_code .br0 {color: #FF00FF;} .mysql.geshi_code .sy1 {color: #CC0099;} .mysql.geshi_code .sy2 {color: #000033;} .mysql.geshi_code .st0 {color: #008000;} .mysql.geshi_code .nu0 {color: #008080;} .mysql.geshi_code span.xtra { display:block; }

SELECT * FROM tableName WHERE tableName.dt BETWEEN '2012-11-01' AND '2012-11-30' ORDER BY tableName.dt DESC
 

как получить список при выборе ip без повторов за день, т.е. получить:
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .php.geshi_code {font-family:monospace;} .php.geshi_code .imp {font-weight: bold; color: red;} .php.geshi_code .kw1 {color: #b1b100;} .php.geshi_code .kw2 {color: #000000; font-weight: bold;} .php.geshi_code .kw3 {color: #990000;} .php.geshi_code .co1 {color: #666666; font-style: italic;} .php.geshi_code .co2 {color: #666666; font-style: italic;} .php.geshi_code .co3 {color: #0000cc; font-style: italic;} .php.geshi_code .co4 {color: #009933; font-style: italic;} .php.geshi_code .coMULTI {color: #666666; font-style: italic;} .php.geshi_code .es0 {color: #000099; font-weight: bold;} .php.geshi_code .es1 {color: #000099; font-weight: bold;} .php.geshi_code .es2 {color: #660099; font-weight: bold;} .php.geshi_code .es3 {color: #660099; font-weight: bold;} .php.geshi_code .es4 {color: #006699; font-weight: bold;} .php.geshi_code .es5 {color: #006699; font-weight: bold; font-style: italic;} .php.geshi_code .es6 {color: #009933; font-weight: bold;} .php.geshi_code .es_h {color: #000099; font-weight: bold;} .php.geshi_code .br0 {color: #009900;} .php.geshi_code .sy0 {color: #339933;} .php.geshi_code .sy1 {color: #000000; font-weight: bold;} .php.geshi_code .st0 {color: #0000ff;} .php.geshi_code .st_h {color: #0000ff;} .php.geshi_code .nu0 {color: #cc66cc;} .php.geshi_code .nu8 {color: #208080;} .php.geshi_code .nu12 {color: #208080;} .php.geshi_code .nu19 {color:#800080;} .php.geshi_code .me1 {color: #004000;} .php.geshi_code .me2 {color: #004000;} .php.geshi_code .re0 {color: #000088;} .php.geshi_code span.xtra { display:block; }

$_var["2012-11-01"]["14-08-23"] = 192.168.0.1;
$_var["2012-11-01"]["13-05-34"] = 192.168.0.2;
$_var["2012-11-02"]["17-01-22"] = 192.168.0.1;
$_var["2012-11-02"]["17-02-45"] = 192.168.0.2;
 

немного пояснения: необходимо получить уникальных посетителей за каждый день в указанном диапазоне дат
============= ДОБАВЛЕНО ПОСЛЕ 5 СИГ**ЕТ ===============
попробовал рабочий вариант: сначало дистинктом вытащил даты, потом через foreach сделал запрос на каждую дату с дистинктом по ip ..... - вариант очень некрасивый - так что жду предложений :)

1 ответов


Не пойму, что вы паритесь.


SELECT distinct dt, ip FROM tableName WHERE tableName.dt BETWEEN '2012-11-01' AND '2012-11-30' ORDER BY tableName.dt DESC
 

А если вам только количество уникальных IP надо, то

SELECT dt, count(distinct ip) FROM tableName WHERE tableName.dt BETWEEN '2012-11-01' AND '2012-11-30'
GROUP BY tableName.dt
ORDER BY tableName.dt DESC
 

DISTINCT и GROUP BY вам в помощь...



SELECT dt,ip,count(ip) FROM tableName WHERE tableName.dt BETWEEN '2012-11-01' AND '2012-11-30'  GROUP BY dt,ip