D
D
Doniyor Mamatkulov2018-10-08 08:35:46
MySQL
Doniyor Mamatkulov, 2018-10-08 08:35:46

How to select data from different tables?

Hi all!
There is such a query in mySQL:

select DISTINCT(COUNT(t1.username))
from radpostauth t1
where exists 
(select *
from radacct t2
where `nasipaddress` = '146.120.17.42' AND t1.authdate between t2.acctstarttime and t2.acctstoptime
) AND `reply` = 'Access-Accept' AND `authdate` BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW();

It returns me the correct number. Now we need to get data by unique username (not COUNT()).
Data to get from `radacct` table:
1. radacctid
2. username
3. acctstarttime
4. acctstoptime
5. framedipaddress

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lander, 2018-10-08
@usdglander

get data by unique username

GROUP BY `username`

S
Stalker_RED, 2018-10-08
@Stalker_RED

SELECT t1.radacctid
  , t1.username
  , t1.acctstarttime
  , t1.acctstoptime
  , t1.framedipaddress
  , COUNT(*) as cnt
FROM radpostauth t1
WHERE exists (
  SELECT *
  FROM radacct t2
  WHERE
      `nasipaddress` = '146.120.17.42' 
      AND t1.authdate between t2.acctstarttime AND t2.acctstoptime
)
AND `reply` = 'Access-Accept' 
AND `authdate` BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW()

GROUP BY t1.`username` -- это важно

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question