我在 MySQL DB 的列之一中有记录,例如:
ID Tax
1 GST + PST + ABC
2 PST + GST + ABC
3 XYZ
4 PST + ABC + GST
这些在数据库中存储为 varchar。通过我的代码,我需要从数据库中获取符合 tax = "GST + PST + ABC"等条件的记录
由于查询,目前我只得到第一条记录:
Select * from table where taxes = "GST + PST + ABC";
但我想从数据库中获取所有记录,其中会出现以上 3 个名称(GST、PST、ABC),而不管它们的位置如何。
所以,如果我应用上述条件,我需要记录:
GST + PST + ABC
PST + GST + ABC
PST + ABC + GST
我正在使用MySQL DB,请告诉我是否有任何功能可以根据我的情况实现上述结果
最佳答案
我会建议这样的事情:
where taxes like '%GST%' and
taxes like '%PST%' and
taxes like '%ABC%' and
length(taxes) = 15;
如果您正好需要这三个代码,这将测试长度。也可以测试+
的个数:
where taxes like '%+%+%' and taxes not like '%+%+%+%'
或者,如果您只需要这些代码而不需要其他代码,则将这些条件排除在外。
这假设名称不重叠(因此没有称为“GS”或“ST”的税)。如果可能的话:
where concat(' ', taxes, ' ') like '% GST %' and
concat(' ', taxes, ' ') like '% PST %' and
concat(' ', taxes, ' ') like '% ABC %';
关于mysql - 需要从 MySQL 中获取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51754553/