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 8145411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:39:17+00:00 2026-06-06T13:39:17+00:00

I’d like to combine several queries into as few as possible. This is for

  • 0

I’d like to combine several queries into as few as possible. This is for a video game I’m writing.
Note
-spawn_id is the id of an entity that creates the bad guy on the level at a given place on a map.
-* from spawns includes the spawn_id, max quantity of bad guys to spawn(quantity), and the type of bad guy to spawn (mob_id)to spawn them and an ID to tell it what type of bad guy to spawn
the game is online so Id rather minimize the number of queries

It would be nice if I could subtract count (number already in game) from quantity(maximum quantity to have in game) where spawn ID are the same and return that all together

Would be even better if I could return the spawn ID quantity to spawn and the type of bad guy together as one row like this

SELECT spawn_id, COUNT(*) as ‘count’ FROM game_moblist GROUP BY spawn_id”
returns…

spawn_id count
======== =====
1         2

TABLE spawns contains

spawn_id quantity mob_id level
======== ======== ====== =====
1        5        1      2

mob_id poitns to TABLE mobs which has

mob_id ...stats for mob_id
====== ===================
1      unique stats for mob#1

I’d like to return something like this:

spawn_id   quantity_to_spawn   mob_id   ...stats for mob_id
========   =================   ======
1          3                   1        unique stats for mob#1

which means I must insert 3 rows selected from mobs which I don’t mind doing a second query for so I can multiply stats by level to create a bad guy in the game that is level 2… essentially level 2 means the stats are twice as high as the number stored in ‘mobs’ table

Here is my code so far I could finish it but I’d rather try to reduce the number of queries first.

    sql= "SELECT spawn_id, COUNT(*) as 'count' FROM game_moblist  GROUP BY spawn_id"
       connection.query(sql, function(err, spawncount, fields) {
          sql= "SELECT  * FROM spawns"
          connection.query(sql, function(err, spawns, fields) {
             for (i=0;i<spawns.length;i++){
                if (spawns[i].next_spawn < new Date().getTime()){
               for (j=0;spawncount.length;j++){
                      if (spawncount[j].spawn_id ==spawns[i].spawn_id&&spawncount[j].count<spawns[i].quantity){
                         quantity_to_spawn = spawns[i].quantity -spawncount[j].count
                         //fetch spawns[i].mob_id mob from mobs table
                         for(k=0;k<quantity_to_spawn;k++){
                         //multiply for level
                         //insert into moblist
                         }
                      }
                   }
                }
             }
          });
       });
  • 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-06T13:39:19+00:00Added an answer on June 6, 2026 at 1:39 pm
    SELECT 
     spawn_id,
     quantity - COUNT(game_moblist.(spawn_id)) AS quantity_to_spawn,
     mobs.*
    FROM spawn_contains
    LEFT JOIN mobs USING (mob_id)
    LEFT JOIN game_moblist USING (spawn_id)
    GROUP BY spawn_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.