Jump to content

PHP and MYSQL join tables and select users id in both tables.


Recommended Posts

Hello, 

 

I have the following function in my class: 

	public function my_teams() {	
	    $sql = "
		SELECT DISTINCT(td.team_id) AS team_id, td.team_name AS team_name, CONCAT(me.First_Name,' ', me.Surname) AS managers_full_name   
		FROM `team_details` AS td
		JOIN `team_players` AS tp
		JOIN `members` AS me
		ON td.managers_user_id = me.id
		WHERE td.managers_user_id = '" . $this->get_user_id() ."'
		OR tp.member_id = '" . $this->get_user_id() ."'
		";	
		$my_teams = $this->db->fetch_all_array($sql);
		return $my_teams;
	}

I have three tables: 

 

team_details 

 

team_players 

 

members 

 

I'm trying to select all teams for which the USER can be the team manager `managers_user_id` and also a player `member_id` in another managers team. 

 

however it seems that my above code is only fetching the team in which the user is the manager and not the teams which the user is a player in. 

 

Is there another way of doing this?

 

Thank you for reading. 

 

DISTINCT is not a function that you can apply to a single field - it applies to the whole row.

 

You haven't defined the join condition for the tables team_details and team_players.

 

You haven't given us your table structure.

  • Like 1
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.