简体   繁体   English

SQL JOIN遇到问题

[英]Having Trouble With SQL JOIN

I'm having trouble with a MySQL JOIN and was wondering if someone could help me out. 我在使用MySQL JOIN时遇到麻烦,想知道是否有人可以帮助我。 Real quick overview on the tables I'm joining. 真正快速概述我要加入的表。

User Table Columns (just the columns that matter in this case): User表列(在这种情况下只是重要的列):

userId | active | role | username | addressId

Address Table Columns (just the columns that matter in this case): Address表列(在这种情况下只是重要的列):

addressId | firstName | lastName

ManagerRepRelationship table ManagerRepRelationship

repId | managerId

Here are the relationships between these tables: 以下是这些表之间的关系:

  • When a new User is created, they get assigned a key (userId) and their address information gets it's own row in Address with a key (addressId). 创建新用户时,将为他们分配一个键(userId),其地址信息将在地址中自己的行中带有一个键(addressId)。 That addressId key gets stores with the users row in the User table. 该addressId键与User表中的users行一起存储。

  • Also, when a user is created they are assigned one of two roles: 'manager' or 'representative'. 同样,在创建用户时,将为他们分配以下两个角色之一:“经理”或“代表”。 If they are a representative, they must be assigned a manager (so obviously, some managers need to be created before any reps can be). 如果他们是代表,则必须为他们分配一个经理(显然,在创建任何代表之前都需要创建一些经理)。 When a rep is assigned to a manager and created, the rep's userID is stored into the ManagerRepRelationship in the repId column, at the same time the manager's userId chosen for this rep is stored in the same row under the managerId column. 当代表分配给经理和创建的,该代表的userID存储到ManagerRepRelationshiprepId列,在经理的同时userId选择了这个代表存储在同一行中的下managerId列。

Here is the trouble I am having. 这是我遇到的麻烦。 When it comes to editing a representative, I want to echo the name of the manager that is assigned to that rep at the top. 在编辑代表时,我想在顶部回显分配给该代表的经理的姓名。 On the edit page, I am passing the edited representative's userId in the url so it is available with $_GET["userID'] . 在编辑页面上,我在URL中传递了经过编辑的代表的userId ,因此可以与$_GET["userID']

I cannot seem to write the correct JOIN statement to get back the first and last name of the manager, not the rep being edited. 我似乎无法编写正确的JOIN语句来获取管理者的名字和姓氏,而不是正在编辑的代表。 Here is what I have so far but I am getting null. 到目前为止,这是我所拥有的,但是我越来越空了。 I hope I explained this well enough. 我希望我解释得足够好。

<?php
  $user_id = $_GET["userId"]; // the rep getting edited userId
  // grab all the reps address info and user info
  $user_result = mysqli_query( $connection,
                "SELECT Users.*, Address.*
                 FROM Users
                  JOIN Address
                  ON Users.addressId=Address.addressId
                 WHERE Users.userId=" . $user_id
              );
  $user_set = mysqli_fetch_array($user_result);

 // Trying to get the first and last name of the manager
 // That is assigned to this rep
   $result = mysqli_query( $connection,
                    "SELECT A.firstName, A.lastName
                     FROM Address A
                      JOIN Users U
                      ON A.addressId=U.addressId
                      JOIN ManagerRepRelationship M
                      ON M.repId=U.userId AND M.managerID=U.userId
                     WHERE U.role='manager' AND M.repId=" . $user_id
                   );
   $managerName = mysqli_fetch_array($result);
?>

This condition: 这种情况:

ON M.repId=U.userId AND M.managerID=U.userId

will only be true if a person is their own manager. 仅当一个人是其自己的经理时才为真。 I think what your looking for is something like this: 我认为您要寻找的是这样的:

"SELECT
   A.firstName, A.lastName
FROM 
   Address A
   JOIN Users U ON A.addressId=U.addressId
   JOIN ManagerRepRelationship M ON M.managerID=U.userId
   WHERE U.role='manager' AND M.repId=" . $user_id

It should also be mentioned that your code is susceptible to SQL Injection and is therefore quite insecure. 还应该提到的是,您的代码容易受到SQL注入的攻击,因此非常不安全。

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

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