简体   繁体   English

在SQL中使用JOIN

[英]Using JOIN in SQL

I'm fairly new to SQL and am trying to get a grasp on the best practices. 我对SQL还是很陌生,正在尝试掌握最佳实践。 I have a question about using the JOIN keyword. 我对使用JOIN关键字有疑问。 Right now, I have a <table> on the front end that displays "practices" from a SQL table, but only if that "practice" has a "representative" that is owned by the current user(the "manager"). 现在,我在前端有一个<table> ,它显示SQL表中的“实践”,但前提是该“实践”具有当前用户(“经理”)拥有的“代表”。 Right now what I am doing is grabbing the "representatives" that belong to the "manager" (the current logged in user) and looping through those to grab the id of the "representative" and push it into an array. 现在,我正在做的是获取属于“经理”(当前登录用户)的“代表”,并循环遍历以获取“代表”的ID并将其推入数组。 When I write my query, I am grabbing all the practices that belong to any of the "representative" id's in that array and building my query that way: 当我编写查询时,我将获取该数组中任何属于“代表” ID的所有实践,并以此方式构建查询:

// Grabbing the reps that are assigned to current user
$owned_reps_id = $database->run_query("SELECT id FROM reps WHERE owner={$_SESSION['current_user_id']}");
$owned_reps_array = array();
while($owned_rep = mysqli_fetch_assoc($owned_reps_id)){
// Throwing non-duplicate values in an array
if(!in_array($owned_rep['id'], $owned_reps_array)){
  array_push($owned_reps_array, $owned_rep['id']);
}
}

// Building the query to only grab "practices" where it's `owner` column matches an id in the $owned_reps_array
$query = "SELECT * FROM practices WHERE";
if(!empty($owned_reps_array)){
  foreach($owned_reps_array as $rep_owner){
    $query .= " owner={$rep_owner}";
    if (end($owned_reps_array) !== $rep_owner){
      $query .= " OR";
    }
  };
};
$query .= " ORDER BY practice_name ASC";
$all_practices = $database->run_query($query);

What I am wondering here is if I took the long way around this. 我想知道的是,我是否在这方面走了很长一段路。 This code does work the way I want it to, but I feel like I went through a lot just to get the result. 这段代码确实按照我想要的方式工作,但是我觉得我花了很多时间才得到结果。 Again, what I am doing is: 同样,我正在做的是:

1) Grab all representatives that have the current manager's id saved in the 'owner' column. 1)抓住所有拥有当前经理ID的代表,并将其保存在“所有者”列中。

2) Of those representatives, then go look into the practices table and grab all the practices that have have any of the id's pulled from the representatives. 2)在这些代表中,然后进入行为表,并从代表那里获取具有任何ID的所有行为。 (Again there is an 'owner' column in the "practices" SQL table that stores the ID of the "representative" it is owned by) (同样,“ practices” SQL表中有一个“所有者”列,用于存储其拥有的“代表”的ID)

Is this something I can do using JOIN or is the route I took efficient? 这是我可以使用JOIN还是我选择的路线有效?

Try executing this query: 尝试执行以下查询:

SELECT 
    p.* 
FROM 
    practices p
    INNER JOIN reps r ON p.owner = r.id
WHERE 
    r.owner=:owner_id 
ORDER BY 
    practice_name

(replace :owner_id with number in your DB query tool) (用数据库查询工具中的数字替换:owner_id

I would be best to replace interpolated $_SESSION with parameters, and do some cleanup, but that's not your question... 我最好用参数替换插值的$_SESSION并进行一些清理,但这不是您的问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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