MYSQL从子查询中选择列表中的项

MYSQL从子查询中选择列表中的项,mysql,Mysql,提前感谢你们提供的任何帮助。今天早上,我已经在这个问题上纠缠了几个小时,我所寻找的似乎没有任何帮助 我正在数据库中以字符串形式存储与客户端关联的帐户列表,如下所示: | li.client_accounts | +-----+----------------+ |ID |facility | +-----+----------------+ |23 |1010, 1020, 1025| +-----+----------------+

提前感谢你们提供的任何帮助。今天早上,我已经在这个问题上纠缠了几个小时,我所寻找的似乎没有任何帮助

我正在数据库中以字符串形式存储与客户端关联的帐户列表,如下所示:

   | li.client_accounts   |
   +-----+----------------+
   |ID   |facility        |
   +-----+----------------+
   |23   |1010, 1020, 1025|
   +-----+----------------+
    SELECT * FROM li_appointments.li_appointments
    where app_client_id in (select facility from li_client_accounts where id = 23)
SELECT * FROM li_appointments.li_appointments
    where app_client_id in ('1010, 1020, 1025')
我正在尝试使用如下子查询从列表中的帐号所在的另一个数据库中选择约会:

   | li.client_accounts   |
   +-----+----------------+
   |ID   |facility        |
   +-----+----------------+
   |23   |1010, 1020, 1025|
   +-----+----------------+
    SELECT * FROM li_appointments.li_appointments
    where app_client_id in (select facility from li_client_accounts where id = 23)
SELECT * FROM li_appointments.li_appointments
    where app_client_id in ('1010, 1020, 1025')

但是,我的结果只显示了客户id为1010的约会,而忽略了其余的约会。我需要做什么才能使它工作?

它不工作,因为
IN
子句需要一个字段列表。但是,在您的例子中,
facility
是由逗号分隔的值组成的字符串,而不是可查询的列表。这是个糟糕的设计。您需要使您的客户账户表原子化,如:

   | li.client_accounts   |
   +-----+----------------+
   |client_ID   |facility |
   +-----+----------------+
   |23   |1010|
   |23   |1015|
   |23   |1020|
   +-----+----------------+
你不能像那样在()中使用

你实际上得到的是这样的东西:

   | li.client_accounts   |
   +-----+----------------+
   |ID   |facility        |
   +-----+----------------+
   |23   |1010, 1020, 1025|
   +-----+----------------+
    SELECT * FROM li_appointments.li_appointments
    where app_client_id in (select facility from li_client_accounts where id = 23)
SELECT * FROM li_appointments.li_appointments
    where app_client_id in ('1010, 1020, 1025')
这显然不是你想要的


如果你想以关系的方式使用你的
工具
列,你需要认真考虑规范化它。

你是对的。我想我是想偷懒我的php代码。谢谢你把我推向正确的方向,你能改变你桌子的设计吗?将多个值粘贴到单个字段中违反了第一个标准形式-这是像这样的查询无法正常工作的原因。