Jump to content

mysql selecting


a1amattyj

Recommended Posts

Hi,

 

I have two tables which both contain 2 columns of the same name (race and gold). Im using an inner join mysql statement.

 

In table 1, .race is an id which corresponds to the second table where .race is a name. I would like to only select the name (table 2).

 

This is what im using:

 

SELECT `races`.race,

`members`.id,

`members`.username,

`members`.gold

FROM `members`

INNER JOIN `races`

ON `members`.race = `races`.idrace

 

However, as the table columns increase, im having to manually add them to the statement (`members`.username, ) for example say i add 'gold2' to the table, i need to add (`members`.gold2,)

 

Any better way to do this?

 

thanks

Link to comment
Share on other sites

Looking at what you have written, I think you may be going about this very inefficiently.

Can you post your table structures showing field names and data types and explain again what you are trying to do, and I will be able to advise on the best approach

Link to comment
Share on other sites

Table 1 Members

 

#  |  id    |  username  |  gold    |  race

1        6            example        50          3

 

Table 2 Races

 

#  |  idRace    |  gold    |  race

46      6345          5            Trees

 

 

Thanks

 

 

Link to comment
Share on other sites

ok, unfortunately I do not see anything in those 2 tables that can be used as a join field. When 2 or more tables are joined, they are joined by having 2 columns of the same data type having the same value.

 

eg an items table with an order_id of 2 can join to an orders table with an id of 2

 

but the fields must be the same type and have the same value

Link to comment
Share on other sites

Sorry I meant:

 

#  |  id    |  username  |  gold    |  race

1        6            example        50          3

 

Table 2 Races

 

#  |  idRace    |  gold    |  race

46      3          5            Trees

 

ok, unfortunately I do not see anything in those 2 tables that can be used as a join field. When 2 or more tables are joined, they are joined by having 2 columns of the same data type having the same value.

 

eg an items table with an order_id of 2 can join to an orders table with an id of 2

 

but the fields must be the same type and have the same value

Link to comment
Share on other sites

ok, that would work as a jon then, but I am a little uncertain as to what you meant when you mentioned adding other fields. If you find yourself needing to add a variable number of the same 'things' then you do not do it by adding columns to an existing table, you create another table to hold the 'things'. I may have misunderstood exactly what you meant when you talked about adding your gold2 etc, but perhaps you could clairify

Link to comment
Share on other sites

So say I added 'gold2' to table 1, i would then need to manually edit my mysql statement as per first post to also select it from the table.

 

Sorry I meant:

 

#  |  id    |  username  |  gold    |  race

1        6            example        50          3

 

Table 2 Races

 

#  |  idRace    |  gold    |  race

46      3          5            Trees

 

ok, unfortunately I do not see anything in those 2 tables that can be used as a join field. When 2 or more tables are joined, they are joined by having 2 columns of the same data type having the same value.

 

eg an items table with an order_id of 2 can join to an orders table with an id of 2

 

but the fields must be the same type and have the same value

Link to comment
Share on other sites

this is what I am asking.  Is your mention of gold2 the start of you wanting to add columns for gold3......n?

 

If so, then adding columns is not the way to go

 

As im developing the software and people request new features, im having to extend the members table to include the new stats. So say they wanted a new option for them to limit attachments, I would add on the table "attachmentlimit", so I would have to alter the statement.

Link to comment
Share on other sites

ok then in that case, this would certainly work for you.

 

select r.race from members m,races r where m.race=r.idrace

 

that is if all you want to get out is the race name from the races table.

You could add more fields to the select as needed.

 

I have used table aliases and an implicit join in that query, which can make it easier to read.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.