简体   繁体   中英

SQL with join two and order by limit

I have the following query that returns the following table:

(select trace_3733_3742.Sequance,trace_3733_3742.MainSequenceNum,trace_3733_3742.hopAddress addr from `trace_3733_3742` where MainSequenceNum = 5668799415 or MainSequenceNum = 5671689631);


+----------+-----------------+------------+
| Sequance | MainSequenceNum | addr       |
+----------+-----------------+------------+
|        1 |      5668799415 | 2229485073 | 
|        3 |      5668799415 | 2229496574 | 
|        4 |      5668799415 | 2258501244 | 
|        5 |      5668799415 | 3286073269 | 
|        6 |      5668799415 | 3241391462 | 
|        7 |      5668799415 | 3241390941 | 
|        8 |      5668799415 | 3241393449 | 
|        9 |      5668799415 | 3241393534 | 
|       10 |      5668799415 | 3561607085 | 
|       11 |      5668799415 |   71666625 | 

... ...

and I have another table that using

(SELECT latitude,longitude FROM `GeoLiteCity_Oct2011` WHERE (start_ip_num <= hopaddress) ORDER BY start_ip_num DESC LIMIT 1);

hopaddress = one of the hopaddress from up table

for example :

(SELECT latitude,longitude FROM `GeoLiteCity_Oct2011` WHERE (start_ip_num <= 3561607199) ORDER BY start_ip_num DESC LIMIT 1);

will return the result i want

the table looks like :

+--------------+------------+----------+-----------+
| start_ip_num | end_ip_num | latitude | longitude |
+--------------+------------+----------+-----------+
|            0 |          0 |     NULL |      NULL | 
|     16777216 |   16777471 |      -27 |       133 | 
|     16777472 |   16778239 |       35 |       105 | 
|     16778240 |   16779263 |      -27 |       133 | 
|     16779264 |   16781311 |       35 |       105 | 
|     16781312 |   16785407 |       36 |       138 | 

... ...

what i want to do is to return a query for the first table with latitude,longitude for each row - but i can't seem to do it

i tried the following :

select * from (select trace_3733_3742.Sequance,trace_3733_3742.MainSequenceNum,trace_3733_3742.hopAddress addr from `trace_3733_3742` where MainSequenceNum = 5668799415 or MainSequenceNum = 5671689631) trace
join GeoLiteCity_Oct2011 loc
where (loc.start_ip_num <= addr) ORDER BY loc.start_ip_num DESC LIMIT 1

but it will only return 1 result and not all the table :(

I would try something like this:

SELECT m.Sequance,m.MainSequenceNum,m.hopAddress,
   (SELECT g.latitude,g.longitude FROM `GeoLiteCity_Oct2011` AS g
    WHERE g.start_ip_num <= m.hopAddress
    ORDER BY g.start_ip_num DESC LIMIT 1)
 FROM `trace_3733_3742` AS m
 WHERE m.MainSequenceNum = 5668799415 or m.MainSequenceNum = 5671689631;

Providing that start_ip_num has unique values you can join GeoLiteCity_Oct2011 table in order to get both lattitude and longitude for start_ip_num selected in your subquery:

SELECT Sequance, MainSequenceNum, hopAddress, latitude, longitude, geo.start_ip_num,
   (SELECT start_ip_num FROM GeoLiteCity_Oct2011
    WHERE start_ip_num <= hopAddress
    ORDER BY start_ip_num DESC LIMIT 1) start_ip
FROM trace_3733_3742
JOIN GeoLiteCity_Oct2011 AS geo
WHERE MainSequenceNum = 5668799415 or MainSequenceNum = 5671689631
HAVING geo.start_ip_num = start_ip;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM