如何在 Mysql 中使用嵌套 SELECT 和 LIKE?

发布于 2024-11-30 16:50:20 字数 233 浏览 2 评论 0原文

假设这是我需要的查询:

SELECT * FROM `products` WHERE `keywords` LIKE "%(SELECT `key` FROM `keywords` WHERE `slug` = '%d8%af%db%8c%d9%88%d8%a7%d8%b1' LIMIT 1)%"

当前它不起作用并返回零结果,而有满足条件的记录。
我想知道这里有什么问题吗?

Assume this is the query I need:

SELECT * FROM `products` WHERE `keywords` LIKE "%(SELECT `key` FROM `keywords` WHERE `slug` = '%d8%af%db%8c%d9%88%d8%a7%d8%b1' LIMIT 1)%"

Currently it doesn't work and returns zero results while there are records that satisfy the condition.
I wonder what is the problem here?

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

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

发布评论

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

评论(3

甜警司 2024-12-07 16:50:20

这应该有效:

SELECT * 
FROM products 
WHERE keywords LIKE
    CONCAT('%', (SELECT `key` FROM `keywords` 
        WHERE `slug` = '%d8%af%db%8c%d9%88%d8%a7%d8%b1' LIMIT 1), '%');

This should work:

SELECT * 
FROM products 
WHERE keywords LIKE
    CONCAT('%', (SELECT `key` FROM `keywords` 
        WHERE `slug` = '%d8%af%db%8c%d9%88%d8%a7%d8%b1' LIMIT 1), '%');
吃兔兔 2024-12-07 16:50:20

试试这个,不知道是否有效...

SELECT * FROM products
WHERE keywords LIKE "%" + (SELECT key FROM keywords WHERE slug = 'something' LIMIT 1) + "%";

Try this one, not sure if it will work...

SELECT * FROM products
WHERE keywords LIKE "%" + (SELECT key FROM keywords WHERE slug = 'something' LIMIT 1) + "%";
囚我心虐我身 2024-12-07 16:50:20

另一个使用类似和嵌套 SELECT 的工作示例:

SELECT COUNT(email) as count FROM table1 t1 
JOIN (
      SELECT company_domains as emailext FROM table2 WHERE company = 'DELL'
     ) t2 
ON t1.email LIKE CONCAT('%', emailext) WHERE t1.event='PC Global Conference";

任务是如果电子邮件扩展名等于多个公司域,则使用过滤器对活动的参与者进行计数。如果有人发现这很有用,那就太好了

Another work example with like and nested SELECT:

SELECT COUNT(email) as count FROM table1 t1 
JOIN (
      SELECT company_domains as emailext FROM table2 WHERE company = 'DELL'
     ) t2 
ON t1.email LIKE CONCAT('%', emailext) WHERE t1.event='PC Global Conference";

Task was to count participants at an event(s) with filter if email extension equal to multiple company domains. Will be great if someone find this useful

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