Copephobia
Copephobia

Reputation: 37

Multiple MySQL queries with different tables in one

So I am working on a game and I would like to make my code a bit easier if possible. I have two tables that I need to query from, one table that I want to return the user id from, and the other table to do some background checking.

Stats table: userid | gold | attack | defense | bonus | ...

Logs table: logid | attacker | attacked | time | ...

Here's the two queries I will probably have:

SELECT userid AS user FROM stats WHERE ($attack + $bonus) > (defense + bonus) ORDER BY gold DESC LIMIT 1

SELECT COUNT(*) AS count FROM logs WHERE attacker = $id AND attacked = user AND date > $time - 86400

the $ variables are php variables of course, and 'user' in the second query refers to the "AS user" in the first query. My goal is to return the userid of the person with the highest gold that I can attack, whom I have attacked less than 5 times in the last day. I'm not quite sure how to join the two, but I would like to if possible!

Upvotes: 0

Views: 220

Answers (1)

shadyyx
shadyyx

Reputation: 16055

Try this (not tested):

SELECT s.userid AS user 
FROM stats s
LEFT JOIN logs l ON (l.attacked = s.userid) // attacked is the one I want to get from stats with the highest gold
WHERE ($attack + $bonus) > (s.defense + s.bonus) 
    AND l.attacker = $id
    AND l.date > $time - 86400
ORDER BY s.gold DESC LIMIT 1

Upvotes: 2

Related Questions