0

I have two tables. Let's call them weapons and weapon_powers.

  • Weapons has ID (primary)
  • weapon powers has ID (primary) and WEAPONTYPE.

WEAPONTYPE is the ID of the weapon the power is assigned to.

A power equipped on weapon 1 would have a weapontype of 1.

How do I combine these two statements into one?

select * from weapons where ID = somevariable

select * from weapon_powers where WEAPONTYPE = somevariable
3
  • 1
    What do you mean by "combine"? What output do you want? Commented Sep 13, 2013 at 1:47
  • I want every record from the first table who's id matches the variable and I also want every record from teh second table who's weapons type also matches that same variable Also I am a mysql noob. Commented Sep 13, 2013 at 1:52
  • What's wrong with the output you get from executing both statements separately? Commented Sep 13, 2013 at 1:53

1 Answer 1

3

If I'm reading your description right, it looks like you have:

WEAPONS   <------->0..*   WEAPON_POWERS
-------                   -------------
ID (PK)                   ? (PK)
...                       WEAPONTYPE (FK WEAPONS(ID))
                          ...

In that case I think you just want a join:

SELECT *
FROM weapons
LEFT OUTER JOIN weapon_powers ON weapons.ID = weapon_powers.WEAPONTYPE
WHERE weapons.ID = @somevariable

This query will return all powers equipped for the given weapon, or a single row with data from weapons and NULL for the weapon_powers.* columns if there are no powers equipped.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.