Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:45:47+00:00 2026-06-18T08:45:47+00:00

My code is this : public function procAllianceAttRanking($limit=) { $q = SELECT . TB_PREFIX

  • 0

My code is this :

public function procAllianceAttRanking($limit="") {
  $q = "SELECT " . TB_PREFIX . "users.id userid, " . TB_PREFIX . "users.username username, " . TB_PREFIX . "users.alliance allyid, (

  SELECT SUM( " . TB_PREFIX . "vdata.pop ) 
  FROM " . TB_PREFIX . "vdata
  WHERE " . TB_PREFIX . "vdata.owner = userid
  )totalpop, (

  SELECT SUM( " . TB_PREFIX . "alidata.Aap ) 
  FROM " . TB_PREFIX . "alidata
  WHERE " . TB_PREFIX . "alidata.id = allyid
  )totalpoint, (

  SELECT COUNT( " . TB_PREFIX . "users.alliance ) 
  FROM " . TB_PREFIX . "users
  WHERE " . TB_PREFIX . "users.alliance = allyid
  )totalusers

  FROM " . TB_PREFIX . "users
  WHERE " . TB_PREFIX . "users.alliance > 0
  ORDER BY totalpoint DESC, allyid ASC $limit";
  return mysql_query($q);
}

and output code is this :

$sql = $ranking->procAllianceRanking();
$query = mysql_num_rows($sql);
if($query >= 1){ 
  while($row = mysql_fetch_array($sql)){
    if($row['allyid'] == $session->alliance) {
      echo "<tr class=\"hl\"><td class=\"ra fc\" >".$rank.".</td>";
    }else {
      echo "<tr class=\"hover\"><td class=\"ra \" >".$rank.".</td>";
    }
    echo "<td class=\"al \" ><a href=\"allianz.php?aid=".$row['allyid']."\">".$database->getAllianceName($row['allyid'])."</a></td>";

    echo "<td class=\"pla \" >".$row['totalusers']."</td>";
        echo "<td class=\"av \">".round($row['totalpop']/$row['totalusers'])."</td>";
      echo "<td class=\"po lc\">".$row['totalpop']."</td></tr>";
    $rank++;
  }
}

And output of the codes is :

Greatest Alliance
   Alliance    player       Ø   points
 1. multii       3          4     11
 2. multii       3          2      6
 3. multii       3          2      6
 4. myallianc    2          5      5
 5. myallianc    2          1      2

but this is not correct !!! . in real travian for each alliance is just one row in statics so the output of my code is wrong . the correct output for each alliance must be like :

Greatest Alliance
   Alliance    player       Ø   points
 1. multii       3          8    23
 2. myallianc    2          6    7

For every alliance must be only one row

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:45:49+00:00Added an answer on June 18, 2026 at 8:45 am

    Your query should be written this way:

    SELECT 
      u.id userid, 
      u.username username, 
      u.alliance allyid, 
      SUM(v.pop )   AS totalpop, 
      SUM(a.Aap ) AS totalpoint,
      COUNT(ua.alliance ) AS totalusers
    FROM users u
    INNER JOIN vdata    v ON v.owner     = u.userid
    INNER JOIN alidata  a ON a.id        = u.allyid
    INNER JOIN users   ua ON ua.alliance = u.allyid
    WHERE users.alliance > 0
    GROUP BY u.id,
             u.username, 
             u.alliance 
    ORDER BY u.totalpoint DESC, 
             u.allyid ASC ;
    

    What I have done in this query:

    I JOINed the tables users, vdata, alidata and users again instead of these correlated subqueries that you used in your question. Note that: I joined the table users one more times to get those users that has alliance = u.allyid but with a different alias. Then GROUP BY, with aggregate functions in the same query.

    You might also need to use LEFT JOIN instead of INNER JOIN in case you want to include those unmtached data, i.e those users that has no entries in the others tables for example.


    Update 1

    To test the query against MySQL directly, and since you are using WAMP. Like in the following steps:

    • Ensure that the WAMP server is running:

    enter image description here

    • From the task par open the phpMyAdmin:

    enter image description here

    • Select your database that you’re working with:

    enter image description here

    • Navigate to SQL Tab:

    enter image description here

    • Paste your query in the window:

    enter image description here

    • Then press the button Go.

    Update 1

    Try this instead:

    SELECT 
      u.id               AS userid, 
      u.username         AS username, 
      u.alliance         AS allyid, 
      SUM(v.pop)         AS totalpop, 
      SUM(a.Aap)         AS totalpoint,
      COUNT(ua.alliance) AS totalusers
    FROM s1_users u
    LEFT JOIN s1_vdata    v ON v.owner     = u.id
    LEFT JOIN s1_alidata  a ON a.id        = u.alliance
    LEFT JOIN s1_users   ua ON ua.alliance = u.alliance
    WHERE u.alliance > 0 
    GROUP BY u.id, 
             u.username, 
             u.alliance;
    

    SQL Fiddle Demo

    This will give you:

    | USERID |    USERNAME | ALLYID | TOTALPOP | TOTALPOINT | TOTALUSERS |
    ----------------------------------------------------------------------
    |      4 | Multihunter |      1 |       18 |          0 |          3 |
    |      5 |     tester1 |      1 |       33 |          0 |          3 |
    |      6 |     tester2 |      1 |       18 |          0 |          3 |
    

    Note that: I couldn’t find a column allyid in the s1_users table, so I joined the table s1_users with s1_alidata with the column alliance and s1_users with itself with the same field alliance. Not sure if this is right or not.


    But there is a big problem in your tables’ design. Your tables are normalized.

    For instance, it seems that the fields Tribe, Access, Gold and Silver are related attributes for each user, so you can move them to a new table something like:

    UsersFooProperties:

    • PropertyId,
    • UserId a foreign key to the users table,
    • Tribe,
    • Access,
    • Gold,
    • Silver,
    • Composite key(ProertyId, userid`).

    The same for the fields B1, B2, B3 and B4, move them to a new table:

    UsersBs:

    • UserId,
    • B1,
    • B2,
    • B3.

    However, if these Bs are more than three and there are other properties for this B proeprty, you can create a new table Bs:

    Bes:

    • BID,
    • other properties here …

    Then:

    UsersBs:

    • UserId,
    • BID,
    • Composite key(Userid, BID).

    Another big issue is with those 38 fields: friend1, … , friend19, friend1wait, …, friend19wait.

    You have to have a separate table for those friends like this:

    `UsersFriends:

    • UserId a foreign key to the users table,
    • FriendID,
    • WaitOrNot a flag 0 or 1.

    There for you have only one column friend, in this column you can insert all the friends of these 38 columns like this:

    UserId FriendId WaitOrNot
      1       1         0
      1       2         1
      1       3         0
      ...
      ...
      1       19        1
    

    Also try to avoid storing mutiple values as a comma separated string value, like what you did in the column FQUEST. Make a new table for it like this:

    UsersFQUEST:

    • UserID,
    • FQUESTId.

    This was just an example of how you can redesign one of your table users these are just a sample of bad things you have in this table, you have other columns than these that I mentioned. You have also to do the same things for other tables.

    For more information, see these:

    • Join (SQL)From Wikipedia.

    • Visual Representation of SQL Joins.

    • Another Visual Explanation of SQL Joins.

    • SQL Queries for Mere Mortals(R): A Hands-On Guide to Data Manipulation in SQL, a great book for SQL Basics.

    • A Simple Guide to Five Normal Forms in Relational Database Theory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: public function getRecurringEventsByGrouped($grouped){ $query = SELECT * FROM `event` AS
I've never seen code like this: public static function getInstance() { if ( !
I have this code to get the default constructor: Public Function f(ByVal t As
I have a similar code snippet like this class Search { public function search($for,
I call this function from with my code-behind: DeleteFile(Server.MapPath(/) + sitemap_index.xml) Public Shared Function
I have this code public function TalentBox(x:int, y:int, arg_color:int = 0xFFFFFF):void { this.graphics.beginFill(arg_color); this.graphics.lineStyle(1.0,
How do I implement my class ClsInterface , which has this code: Public Function
I have this code public function getList() { $array = array(); $r = mysql_query(
I was following this tutorial when typing up this code: public function search($term){ $filter
I have this formula in a cell: =GetData(Channel_01,Chicago) Which executes this code: Public Function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.