简体   繁体   English

从 SQL 数据库中选择存在具有用户 ID 的行,然后获取具有相似值的所有其他行

[英]Selecting from SQL database where row exists with user id then get all other rows with similar value

I'm not sure why but I'm hitting an absolute wall trying to come up with this select statement.我不知道为什么,但我试图想出这个 select 声明绝对是一堵墙。 Maybe there is a PHP/MYSQL function that I'm not familiar with that would help.也许有一个我不熟悉的 PHP/MYSQL function 会有所帮助。 The idea is simple for this user management software: there are managers, and managers can (but do not have to) share clients.这个用户管理软件的想法很简单:有管理员,管理员可以(但不必)共享客户端。 Amongst the manager and shared client relationship, one of the managers can be assigned as a lead.在经理和共享客户关系中,可以将其中一位经理指定为领导。 So here's how the basic example of what the database looks like for 1 client that is shared between 2 managers and assigned, and another client that is also shared but NOT assigned (represented by zero).因此,以下是数据库外观的基本示例,其中 1 个客户端在 2 个经理之间共享并分配,另一个客户端也共享但未分配(用零表示)。

DROP TABLE IF EXISTS clients;

CREATE TABLE clients
(client_id SERIAL PRIMARY KEY
,client_name VARCHAR(12) UNIQUE
);

INSERT INTO clients VALUES
(555,'Jimmy'), 
(789,'Tyler'); 

DROP TABLE IF EXISTS managers;

CREATE TABLE managers
(manager_id SERIAL PRIMARY KEY
,manager_name VARCHAR(12)UNIQUE
);

INSERT INTO managers VALUES
(123,'Michael'),
(456,'David');

DROP TABLE IF EXISTS relationships;

CREATE TABLE relationships
(client_id INT NOT NULL
,manager_id INT NOT NULL
,assigned INT NOT NULL
,PRIMARY KEY(client_id,manager_id)
);

INSERT INTO relationships VALUES
(555, 123, 0),
(555, 456, 1),
(789, 123, 0),
(789, 456, 0);

To get to the point: the statement I'm trying to make is for a manager to be shown all the clients that he has a relationship with, but are NOT assigned to him or anyone else on his team, ie select all of my clients where no one is assigned as the lead.言归正传:我要向经理展示与他有关系但未分配给他或他团队中的任何其他人的所有客户,即 select 我所有的客户没有人被指定为领导。

Expected input: Show all clients that manager 123 has a relationship with, but have yet to be assigned to any manager Expected result: client 789预期输入:显示与经理 123 有关系但尚未分配给任何经理的所有客户 预期结果:客户 789

Happy to clarify as I can see this being overtly confusing as described.很高兴澄清,因为我可以看到这如所描述的那样明显令人困惑。

So you will have to start with finding all clientid's from the manager in RELATIONS, but remove all that have a manager assigned already.因此,您必须首先从 RELATIONS 中的经理那里找到所有 clientid,但删除所有已经分配了经理的。

Depending on the size of the table you might want to rewrite this, but here is one approach:根据表的大小,您可能想要重写它,但这是一种方法:

1) Get all clientids that have no manager: 1)获取所有没有经理的客户端:

SELECT R1.client_id, SUM(R1.assigned) as sumassigned FROM relationships AS R1 GROUP BY R1.client_id HAVING ( SUM(R1.assigned) = 0)

Now it is easier, you just join, eg:现在更容易了,您只需加入,例如:

SELECT R2.client_id, R2.manager_id FROM relationships AS R2
INNER JOIN 
(SELECT R1.client_id, SUM(R1.assigned) as sumassigned FROM relationships AS R1 GROUP BY R1.client_id HAVING ( SUM(R1.assigned) = 0) ) AS DRVNOMANAGER
ON (R2.client_id = DRVNOMANAGER.client_id)
WHERE (R2.manager_id = 123)

Not tested.未测试。 (meaning you might have to fix it) (这意味着您可能必须修复它)

The idea is to create a derived (temp) table containing all clients without a manager, then do an inner join on your original question ("what clients does this manager 123 know that do not have another assigned)这个想法是创建一个包含所有没有经理的客户的派生(临时)表,然后对您的原始问题进行内部联接(“这个经理 123 知道哪些客户没有分配另一个客户)

Does that solve your problem?这能解决你的问题吗?

PS: Such things are much easier solved in PHP, but if your dataset is huge, that is not feasible. PS:这样的事情在 PHP 中更容易解决,但如果你的数据集很大,那是不可行的。

OP asked for clientnames, so just add that: OP 要求提供客户名称,因此只需添加:

        SELECT R2.client_id, R2.manager_id, C.client_name  FROM relationships AS R2
INNER JOIN 
        (SELECT R1.client_id, SUM(R1.assigned) as sumassigned FROM relationships
 AS R1 GROUP BY R1.client_id HAVING ( SUM(R1.assigned) = 0) ) AS DRVNOMANAGER
        ON (R2.client_id = DRVNOMANAGER.client_id)
    INNER JOIN clients AS C ON (C.client_id = R2.client_id)
        WHERE (R2.manager_id = 123)

And last request: removed deleted relations:最后一个请求:删除已删除的关系:

Simply add a WHERE clause to the inner DRVNOMANAGER with your restriction on the rows that are used in the GROUP BY.只需将 WHERE 子句添加到内部 DRVNOMANAGER,并限制 GROUP BY 中使用的行。 eg:例如:

SELECT R2.client_id, R2.manager_id, C.client_name  FROM relationships AS R2
INNER JOIN 
        (SELECT R1.client_id, SUM(R1.assigned) as sumassigned FROM relationships
 AS R1 WHERE (NOT(R1.deleted = 1) ) GROUP BY R1.client_id HAVING ( SUM(R1.assigned) = 0) ) AS DRVNOMANAGER
        ON (R2.client_id = DRVNOMANAGER.client_id)
    INNER JOIN clients AS C ON (C.client_id = R2.client_id)
        WHERE (R2.manager_id = 123)

============================================== ===============================================

THIS WAS MY OLD ANSWER.这是我的老答案。 NOT RELEVANT ANYMORE.不再相关。

"select all of my clients where no one is assigned as the lead." “选择我的所有客户,其中没有人被指定为潜在客户。”

If I read you well that means: Get all clientid from RELATIONS where some managerid is given, AND assigned=0.如果我读得很好,这意味着:从关系中获取所有客户端 ID,其中给出了一些 managerid,并且分配了=0。 (assigned=0 meaning "no one is assigned as the lead.") (assigned=0 表示“没有人被指定为领导。”)

Is that correct?那是对的吗?

Then you end up with something like this (for managerid 123):然后你会得到这样的结果(对于 managerid 123):

SELECT R.clientid, C.clientname FROM RELATIONSHIPS AS R WHERE ( (R.managerid = 123) AND (R.assigned=0))
INNER JOIN CLIENTS AS C ON (C.clientid = R.clientid)

I removed the spaces in the columnnames because I hate spaces in columnnames.我删除了列名中的空格,因为我讨厌列名中的空格。

SELECT c.* 
  FROM managers m 
  JOIN relationships r 
    ON r.manager_id = m.manager_id 
  JOIN clients c 
    ON c.client_id = r.client_id 
  LEFT 
  JOIN relationships x 
    ON x.client_id = c.client_id 
   AND x.assigned = 1 
 WHERE m.manager_id = 123 
   AND r.assigned = 0 
   AND x.client_id IS NULL;
+-----------+-------------+
| client_id | client_name |
+-----------+-------------+
|       789 | Tyler       |
+-----------+-------------+

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

相关问题 Eloquent:获取列值(x)存在于多于一行的所有行(SQL DB) - Eloquent: Get all rows where column value(x) exists in more than one row (SQL DB) 如何获取所有行,其中特定列=不同表中另一行的ID - how to get all rows where specific column = id of other row in different table MySql获取id = xxx且id相同的行中其他表中的最新列大于一个的所有行 - MySql get all rows where id = xxx and where the newest column in other table in row with same id is greater than one 从数据库中选择一行,然后为选项卡活动问题选择其他行 - Selecting one row from database then other rows for tabs active issue PHP,SQL-在表ID =用户ID的地方获取数据,在行=用户ID的地方计算其他表的数据 - PHP, SQL - getting fetch where table id = user id and count other table where row is = user id 更新与用户登录ID匹配的SQL行 - Updating SQL rows where row matches user log in ID 在codeigniter中获取id=$id的数据库中的所有行 - Get all rows form database where id=$id in codeigniter SQL-获取行数并选择某个行ID并获取以下行 - Sql - Getting the row quantity and selecting a certain row id and get the following rows SQL-选择彼此相似的%…%的所有行(所有相似的行) - SQL - Select ALL rows that are LIKE%…% of each other (All Similar Rows) 设置SQL中id ='$ id'的所有行 - Set all rows in SQL where id='$id'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM