MYSQL问题-获取唯一值
假设我在 MySQL 中有这两个变量,
SET data1 = "1,2,3,apple,4,5";
SET data2 = "apple,orange,5";
如何获取这两个变量上出现的项目数?
Let's say I have these two variables in MySQL
SET data1 = "1,2,3,apple,4,5";
SET data2 = "apple,orange,5";
How do I get number of items that appears on those two variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你确实需要在 MySQL 端执行此操作,你可以创建存储过程(或存储函数),其中:
但是这个解决方案确实是离优雅还差得很远。将标签存储在单独的表中(如上所述,每行一个标签)是更可取的。
If you really need to do this on MySQL-side you can create stored procedure (or stored function) in which:
But this solution is really far from being elegant. Storing tags in separate table (as mentioned above, one tag per row) is much more preferable.
没有办法分解列表并计算项目数。以您的母语操作值来分解它的唯一方法:
php 中的示例:
另一种方法是将所有标签放在单独的表中并将它们连接到数据透视表中,然后您可以执行以下操作:
There is no way to explode the list and count the items. The only way to to explode it in you native language manipulating the value:
Example in php:
The other way is to put all tags in a separate table and joining them in a pivot table then you can do something like this: