mysql - SQL Select with multiple search parameters using joins and subqueries -
i have spent hours searching answer problem without satisfying results.
i want select 1 query players, villages , alliances -tables , date , population histories table.
selection must filtered following rules:
- select latest information date.
- select if player has <= number of villages @ moment.
select if total population of player's villages <= @ moment
- and 3. ones causing head hurt. how add query?
here current query:
select players.name player, players.uid uid, players.tid, villages.name village, villages.vid vid, villages.fid fid, alliances.name alliance, alliances.aid aid, sqrt( pow( least(abs($xcoord - villages.x), 400-abs($xcoord - villages.x)), 2 ) + pow( least(abs($ycoord - villages.y), 400-abs($ycoord - villages.y)), 2 ) ) distance histories left join players on players.uid = histories.uid left join villages on villages.vid = histories.vid left join alliances on alliances.aid = histories.aid left join histories h2 on ( histories.vid = h2.vid , histories.idhistory < h2.idhistory ) h2.vid null , histories.uid != $uid , sqrt( pow(least(abs($xcoord - villages.x), 400-abs($xcoord - villages.x)), 2 ) + pow(least(abs($ycoord - villages.y), 400-abs($ycoord - villages.y)), 2 ) ) < $rad order distance
notice: xcoord , ycoord posted search form.
example output:
player| village | alliance | distance p1 | v1 | a1 | 1 p2 | v4 | a2 | 2 p1 | v2 | a1 | 3 p1 | v3 | a1 | 4 p2 | v5 | a2 | 5
thank in advance helping. :)
this query can find players have less 2 villages. cant put original query , together. possible?
select b.*, count(b.uid) hasvillages histories b b.vid in (select a.vid villages a) group b.uid having count(b.uid) < 2
after 1 week of try-outs have found answer. query can use following search parameters:
- find latest rows date
- find rows limiting number of villages player has.
- find rows limiting total population of villages player has.
- find rows calculating distance.
- exclude players or alliances selection.
here query
select players.name player, players.uid uid, players.tid, villages.name village, villages.vid vid, villages.fid fid, alliances.name alliance, alliances.aid aid, sqrt( pow( least(abs(100 - villages.x),400-abs(100 - villages.x)), 2 ) + pow( least(abs(100 - villages.y),400-abs(100 - villages.y)), 2 ) ) distance histories left join players on players.uid = histories.uid left join villages on villages.vid = histories.vid left join alliances on alliances.aid = histories.aid histories.uid in (select b.uid histories b (b.vid in (select a.vid villages a) , b.date in (select max(date) histories)) group b.uid having count(b.uid) < 4 , sum(b.population) < 2000) , histories.uid != 1 , histories.date in (select max(date) histories) , sqrt( pow( least(abs(100 - villages.x),400-abs(100 - villages.x)),2)+ pow( least(abs(100 - villages.y),400-abs(100 - villages.y)), 2 ) ) < 200 order distance
Comments
Post a Comment