如何在MySQL中使用LIKE?

发布于 2024-12-18 14:58:31 字数 643 浏览 0 评论 0原文

我有一个名为 music 的表,其中包含一些 id:

 6983887641 59088763306 116542622632 106436032725763 6750402929 131572223581891 17337462361 56848544614 108089415891178 27659711968 182011613865 8888384363 43780515976 7872609449 38858586087 107901192572009 9028468518 5461947317 11955325695 64075031411 12673567174

我需要对这些 id 进行查询。如果执行了这样的查询:

SELECT username FROM music WHERE music LIKE '%107901192572009 9028468518%'

它将返回音乐表中具有这些 id 的用户的用户名。好的。但问题是如果我以不同的顺序添加 id 则没有结果。所以如果我搜索:

56848544614 107901192572009 or 5461947317 9028468518

它们在表中,但不按顺序,并且没有结果。我应该怎么办?

I got a table called music with some ids:

 6983887641 59088763306 116542622632 106436032725763 6750402929 131572223581891 17337462361 56848544614 108089415891178 27659711968 182011613865 8888384363 43780515976 7872609449 38858586087 107901192572009 9028468518 5461947317 11955325695 64075031411 12673567174

And i need to do a query on those ids. If a query like this had been done:

SELECT username FROM music WHERE music LIKE '%107901192572009 9028468518%'

It would return with the username of the one that has these ids in it's music table. Ok. but the problem is if i add ids in different order there is no result. so if i searched for:

56848544614 107901192572009 or 5461947317 9028468518

They are in the table, but not in the order, and there is no result. What should i do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一城柳絮吹成雪 2024-12-25 14:58:31
SELECT username FROM music
WHERE music LIKE '%107901192572009%'
  AND music LIKE '%9028468518%'

请注意,它会很慢。另请注意,它可能会失败,因为 id 可以是另一个 id 的一部分。有一个更好的方法可以做到这一点 - 查找 1 对 n 关系。如果没有,至少考虑这样做(解决第二个问题):

SELECT username FROM music
WHERE ' ' + music + ' ' LIKE '% 107901192572009 %'
  AND ' ' + music + ' ' LIKE '% 9028468518 %'
SELECT username FROM music
WHERE music LIKE '%107901192572009%'
  AND music LIKE '%9028468518%'

Note that it will be slow. Also note that it can misfire, since an id can be a part of another id. There is a better way to do this - look up 1-to-n relations. If not, at least consider doing this (to combat the second problem):

SELECT username FROM music
WHERE ' ' + music + ' ' LIKE '% 107901192572009 %'
  AND ' ' + music + ' ' LIKE '% 9028468518 %'
巴黎夜雨 2024-12-25 14:58:31

我认为你不想要像你想要的那样

 SELECT username FROM music WHERE music IN (6983887641, 59088763306, 116542622632, 106436032725763);

I think you do not want LIKE you want IN

 SELECT username FROM music WHERE music IN (6983887641, 59088763306, 116542622632, 106436032725763);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文