简体   繁体   中英

DB select two Tables

I've searched around and can't seem to understand how to get this to work. At least, not the way my code is written. What im having trouble doing is changing this:

// ...
$sql3 = new db;
$sql3->db_Select(DB_TABLE_ROSTER_TEAM_MEMBERS, "*", "team_id = ".intval($row1['team_id'])." ORDER BY member_team_order");

while($row3 = $sql3->db_Fetch()) {
// ...

...to something that orders by the column status_order , which is located in a different table DB_TABLE_ROSTER_MEMBER_STATUS . So essentially I'm trying to include 2 tables in the same SELECT query, so that I can change the ORDER BY . Can anyone explain how this is done? Any help would be greatly appreciated. Thank you.

UPDATE

I'm going to try and explain this as much as I can. Hopefully someone will be able to explain to me in a way I can understand.

By default, this code is meant to grab the users and place them in their respective "team_id", then order them by a selectable drop down box that allows me to change the order. However, the part that orders the members is not working, so I decided I wanted to have it automatically order by the member status that is located in the other table. The other table holds the team names and team order, along with other data that is not needed right now. I didnt even know if this was possible.

There is another code a little further down that may help as well, as it shows the structure of the table. It's what gives the names color as seen on the site I mentioned.

Hopefully I've helped explain it a bit more.

And some more requested by Johan

Here is the structure of the two tables.

roster_team_members fields:

member_id
member_name
team_id
team_name
game_id
game_name
member_team_status
text_color
member_team_order

roster_member_status fields:

status_id
status_name
text_color
status_order
display

Iv decided to fix the manual member order. iv found an error while debugging. if i cant figure out how to fix it il pot another question. thank you all for your help. iv just decided to go another route that should prove to be easier.

What does your db_select() method look like? Are you using Drupal?

Your raw SQL query would look something like this:

SELECT table1.* FROM table1, table2 WHERE table1.link_column = table2.link_column AND table1.team_id = 'Your team ID' ORDER BY table2.member_team_order

Without knowing exactly how your db_select() method works I can't tell you exactly how to do this.

Try this query:

select a.*,b.rank
from DB_TABLE_ROSTER_TEAM_MEMBERS a INNER JOIN DB_TABLE_ROSTER_MEMBER_STATUS b
ON a.member_id = b.member_id
order by b.rank;

I've had to make a guess that the linking column between DB_TABLE_ROSTER_TEAM_MEMBERS and DB_TABLE_ROSTER_MEMBER_STATUS is called 'member_id' and that the column that you wish to rank by in the DB_TABLE_ROSTER_MEMBER_STATUS is called 'rank'.

In order to join two tables you use a join clause.

Something like:

SELECT booking.`date`, customer.name 
FROM booking
INNER JOIN customer ON (customer.id = booking.customer_id)
WHERE customer.name = 'Sarge' 
ORDER BY booking.`date` DESC

If you have to tables with the same layout you can use a UNION .

  SELECT customer_USA.name, customer_USA.country
  FROM customer_USA
UNION ALL
  SELECT customer_NL.name, customer_NL.country
  FROM customer_NL

This will merge all records for both tables in one big resultset.

I'd give you the proper query for your case, but I have no idea what you want and the links you've provided require too much study, killing the fun of the puzzle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM