Mysql 如果coumn_name有逗号分隔的值,如何运行sql查询?
获取14个数据的查询是什么? 我试过了Mysql 如果coumn_name有逗号分隔的值,如何运行sql查询?,mysql,sql,Mysql,Sql,获取14个数据的查询是什么? 我试过了 CustomerID cat_id ContactName Address City PostalCode Country 1 Alfreds 13 Maria Anders Obere Str Berlin 12209 Germany 4 Around 13,14 Thomas Hardy 120 Hanov London WA1 1DP UK 但失败了 您可以使用: 注意:你不应该这样存储价值
CustomerID cat_id ContactName Address City PostalCode Country
1 Alfreds 13 Maria Anders Obere Str Berlin 12209 Germany
4 Around 13,14 Thomas Hardy 120 Hanov London WA1 1DP UK
但失败了 您可以使用:
注意:你不应该这样存储价值
您的表应该如下所示,以避免使用
FIND\u IN\u SET
解决方案:
表客户
:
SELECT *
FROM `customers`
WHERE FIND_IN_SET(13, `cat_id`) > 0 OR FIND_IN_SET(14, `cat_id`) > 0
CustomerID | ContactName | Address | City | PostalCode | Country
1 | Maria Anders | Obere Str | Berlin | 12209 | Germany
4 | Thomas Hardy | 120 Hanov | London | WA1 1DP | UK
customer_id | cat_id
1 | 13
4 | 13
4 | 14
表客户\u猫
:
SELECT *
FROM `customers`
WHERE FIND_IN_SET(13, `cat_id`) > 0 OR FIND_IN_SET(14, `cat_id`) > 0
CustomerID | ContactName | Address | City | PostalCode | Country
1 | Maria Anders | Obere Str | Berlin | 12209 | Germany
4 | Thomas Hardy | 120 Hanov | London | WA1 1DP | UK
customer_id | cat_id
1 | 13
4 | 13
4 | 14
表格cats
:
SELECT *
FROM `customers`
WHERE FIND_IN_SET(13, `cat_id`) > 0 OR FIND_IN_SET(14, `cat_id`) > 0
CustomerID | ContactName | Address | City | PostalCode | Country
1 | Maria Anders | Obere Str | Berlin | 12209 | Germany
4 | Thomas Hardy | 120 Hanov | London | WA1 1DP | UK
customer_id | cat_id
1 | 13
4 | 13
4 | 14
因此,查询如下所示:
CatID | Column1
---------------
13 | abc
14 | def
您可以使用:
注意:你不应该这样存储价值
您的表应该如下所示,以避免使用
FIND\u IN\u SET
解决方案:
表客户
:
SELECT *
FROM `customers`
WHERE FIND_IN_SET(13, `cat_id`) > 0 OR FIND_IN_SET(14, `cat_id`) > 0
CustomerID | ContactName | Address | City | PostalCode | Country
1 | Maria Anders | Obere Str | Berlin | 12209 | Germany
4 | Thomas Hardy | 120 Hanov | London | WA1 1DP | UK
customer_id | cat_id
1 | 13
4 | 13
4 | 14
表客户\u猫
:
SELECT *
FROM `customers`
WHERE FIND_IN_SET(13, `cat_id`) > 0 OR FIND_IN_SET(14, `cat_id`) > 0
CustomerID | ContactName | Address | City | PostalCode | Country
1 | Maria Anders | Obere Str | Berlin | 12209 | Germany
4 | Thomas Hardy | 120 Hanov | London | WA1 1DP | UK
customer_id | cat_id
1 | 13
4 | 13
4 | 14
表格cats
:
SELECT *
FROM `customers`
WHERE FIND_IN_SET(13, `cat_id`) > 0 OR FIND_IN_SET(14, `cat_id`) > 0
CustomerID | ContactName | Address | City | PostalCode | Country
1 | Maria Anders | Obere Str | Berlin | 12209 | Germany
4 | Thomas Hardy | 120 Hanov | London | WA1 1DP | UK
customer_id | cat_id
1 | 13
4 | 13
4 | 14
因此,查询如下所示:
CatID | Column1
---------------
13 | abc
14 | def
修正你的数据模型!为什么错了
- 数值应存储在类型正确的列中,而该列不是字符串
- 这些看起来像身份证。应正确定义外键关系
- SQL具有糟糕的字符串处理能力
- 使用字符串操作的查询通常无法优化,因此无法优化索引、表统计信息和分区
- SQL有这样一个非常好的数据结构来存储列表。它叫桌子
customer\u id
和cat\u id
应该有一个表。这通常被称为“连接表”或“关联表”
也就是说,有时我们会被其他人的设计决策所困扰,真的,真的,真的,真的很糟糕。如果是这样,您可以使用find\u in\u set()
执行所需操作:
修正你的数据模型!为什么错了
- 数值应存储在类型正确的列中,而该列不是字符串
- 这些看起来像身份证。应正确定义外键关系
- SQL具有糟糕的字符串处理能力
- 使用字符串操作的查询通常无法优化,因此无法优化索引、表统计信息和分区
- SQL有这样一个非常好的数据结构来存储列表。它叫桌子
customer\u id
和cat\u id
应该有一个表。这通常被称为“连接表”或“关联表”
也就是说,有时我们会被其他人的设计决策所困扰,真的,真的,真的,真的很糟糕。如果是这样,您可以使用find\u in\u set()
执行所需操作:
像这样的方法应该会奏效:
where find_in_set(13, cat_ids) > 0 and -- or is that "or"
find_in_set(14, cat_ids) > 0
虽然你应该考虑规范它。 < P>这样的事情应该起作用:
where find_in_set(13, cat_ids) > 0 and -- or is that "or"
find_in_set(14, cat_ids) > 0
虽然应该考虑规范化。< /P>不要将外键存储为CSV。了解规范化。同时使用find_in_set(),我知道。但是我希望解决方案不要将外键存储为csv。了解规范化。同时使用find_in_set(),我知道。但我想要那个解决方案