Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要帮助缩短mysql多个或多个条件吗_Mysql - Fatal编程技术网

需要帮助缩短mysql多个或多个条件吗

需要帮助缩短mysql多个或多个条件吗,mysql,Mysql,我有一个包含经理id和员工id(逗号分隔)的表,我有以下sql语句: SELECT manager_id, staff_ids FROM manager_staffs WHERE 1 IN (staff_ids) OR 3 IN (staff_ids) OR 5 IN (staff_ids) OR 23 IN (staff_ids) OR 12 IN (staff_ids) OR 16 IN (staff_ids) OR 19 IN (staff_ids) OR 32 IN (staff_id

我有一个包含经理id和员工id(逗号分隔)的表,我有以下sql语句:

SELECT manager_id, staff_ids FROM manager_staffs WHERE 1 IN (staff_ids) OR 3 IN (staff_ids) OR 5 IN (staff_ids) OR 23 IN (staff_ids) OR 12 IN (staff_ids) OR 16 IN (staff_ids) OR 19 IN (staff_ids) OR 32 IN (staff_ids) OR 123 IN (staff_ids) ............ 选择经理id、员工id 来自经理和员工 其中1英寸(员工ID) 或3英寸(员工ID) 或5英寸(员工ID) 或23英寸(员工ID) 或12英寸(员工ID) 或16英寸(员工ID) 或19英寸(员工ID) 或32英寸(员工ID) 或123英寸(员工ID) ............ 基本上,这是一个很长的查询,有很多“或”条件。如果有办法,我想改进或缩短它

需要所有sql专家的帮助。

为什么不试试这个

    SELECT manager_id, staff_ids
   FROM manager_staffs 
    WHERE staff_ids IN (1,3,5,23,12,16,19,32,123,.......)
  • 只有在创建其他表并像上面的查询一样选择它们时,才能选择如何减少
    s

  • 结构表是检索数据时获得性能的最佳方法

你的桌子应该是那样的

 manager_staff 
 manager_id , staff_id
   1            1
   1            3
   1            5
   1            23
   ..........

我不确定你想要实现什么,但试试这个:

选择经理id、员工id 来自经理和员工 其中工作人员ID位于('1','3'等)


假设staff_id是逗号分隔的字段,则可以执行以下操作

SELECT DISTINCT manager_id, staff_ids
FROM manager_staffs a
INNER JOIN (SELECT 1 AS staff_id UNION SELECT 3 UNION SELECT 5 UNION SELECT 23 UNION SELECT 12 UNION SELECT 16 UNION SELECT 19 UNION SELECT 32 UNION SELECT 123) b
ON FIND_IN_SET(b.staff_id, a.staff_ids) > 0

但正如我之前所评论的,将其拆分将是一个更好的主意

问题在于您的数据结构-不要将所有员工ID放在一列中。制作一张桌子-你可以称之为经理或职员:

CREATE TABLE manager_staff (
     manager_id int,
     staff_id int
);
因此,您可以制作记录,显示每个员工向哪个经理报告。然后您的查询变成:

select manager_id, staff_id from manager_staff where staff_id in (1,3,...);

我认为这行不通。据他说,员工ID用逗号分隔是的。它们是逗号分隔的。这是您的结构错误。您应该创建另一个带有staff_ID的表,然后通过我的查询获得它们。对不起,表结构无法更改。它已经存在这么多年了,有这么多的页面在使用该表。staff_ID是一个包含逗号分隔的staff ID列表的字段吗?如果是这样的话,除非经理只有一名员工,否则您的查询似乎不起作用。如果它是一个逗号分隔的列表,那么您应该将列表拆分到另一个表中,每个工作人员在新表中有一行。很抱歉我希望我能改变。