使用csv从另一个表的列b中获取表a列中的值

e0bqpujr  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(264)

我有两张这样的table:

Table Items
+--------------------+
| id | item          |
+----+---------------+
| 1  | Table         |
+----+---------------+
| 2  | couch         |
+----+---------------+
| 3  | Bed           |
+----+---------------+
| 4  | Chair         |
+----+---------------+
| 5  | Desk          |
+----+---------------+

Table Acme
+--------------------+--------+
| id | items         | Client |
+----+---------------+--------+
| 1  | 1,3,5         | S45-56 |
+----+---------------+--------+

我需要的是使用acme表中的csv来选择items表中的项目
我尝试使用以下查询,但只得到树值中的第一个

SELECT * FROM Items WHERE id IN (SELECT items FROM Acme WHERE client ='S45-56')

你知道吗?

cx6n0qe3

cx6n0qe31#

你可以用 find_in_set() 检查值是否在逗号分隔的列表中。

SELECT i.*
       FROM items i
            INNER JOIN acme a
                       ON find_in_set(i.id, a.items)
       WHERE a.client = 'S45-56';

但最好修复您的模式,不要使用逗号分隔的列表,而是在一个表中使用多行。

相关问题