php - Using JOIN in SQL -


i'm new sql , trying grasp on best practices. have question using join keyword. right now, have <table> on front end displays "practices" sql table, if "practice" has "representative" owned current user(the "manager"). right doing grabbing "representatives" belong "manager" (the current logged in user) , looping through grab id of "representative" , push array. when write query, grabbing practices belong of "representative" id's in array , building query way:

// grabbing reps assigned current user $owned_reps_id = $database->run_query("select id reps owner={$_session['current_user_id']}"); $owned_reps_array = array(); while($owned_rep = mysqli_fetch_assoc($owned_reps_id)){ // throwing non-duplicate values in array if(!in_array($owned_rep['id'], $owned_reps_array)){   array_push($owned_reps_array, $owned_rep['id']); } }  // building query grab "practices" it's `owner` column matches id in $owned_reps_array $query = "select * practices where"; if(!empty($owned_reps_array)){   foreach($owned_reps_array $rep_owner){     $query .= " owner={$rep_owner}";     if (end($owned_reps_array) !== $rep_owner){       $query .= " or";     }   }; }; $query .= " order practice_name asc"; $all_practices = $database->run_query($query); 

what wondering here if took long way around this. code work way want to, feel went through lot result. again, doing is:

1) grab representatives have current manager's id saved in 'owner' column.

2) of representatives, go practices table , grab practices have have of id's pulled representatives. (again there 'owner' column in "practices" sql table stores id of "representative" owned by)

is can using join or route took efficient?

try executing query:

select      p.*       practices p     inner join reps r on p.owner = r.id      r.owner=:owner_id  order      practice_name 

(replace :owner_id number in db query tool)

i best replace interpolated $_session parameters, , cleanup, that's not question...


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -