gpt4 book ai didi

MySQL查询关系表

转载 作者:行者123 更新时间:2023-12-01 00:06:52 25 4
gpt4 key购买 nike

我想这或多或少是微不足道的事情,但事情就是这样......

我有几个关系表,我正在尝试根据 2 个值检索行。到目前为止,这是我的查询:

SELECT `inventory`.*, `tags`.*, `tags_inv`.*, `categories`.*, `categories_inv`.*
FROM (`tags`)
LEFT JOIN `tags_inv` ON `tags_inv`.`tag_id` = `tags`.`tag_id`
LEFT JOIN `inventory` ON `inventory`.`inventory_id` = `tags_inv`.`inv_id`
LEFT JOIN `categories_inv` ON `categories_inv`.`inv_id` = `inventory`.`inventory_id`
LEFT JOIN `categories` ON `categories`.`cat_id` = `categories_inv`.`cat_id`
WHERE `categories`.`cat_id` = 1
AND `inventory`.`inv_name` LIKE '%p%'
OR `inventory`.`inv_dim_w` LIKE '%p%'
OR `inventory`.`inv_dim_l` LIKE '%p%'
OR `inventory`.`inv_dim_h` LIKE '%p%'
OR `tags`.`tag_description` LIKE '%p%'
ORDER BY `inventory`.`inv_name`

问题是我的查询从我的数据库中选择了所有内容(就像它忽略了 cat_id),但在这种情况下我只想选择带有 cat_id 1 的行...
有没有快速合理的方法来解决这个问题? :)

提前致谢!

最佳答案

在 WHERE 子句中使用括号将所有 OR 条件组合在一起。因为您在 WHERE 的同一级别上有 AND 和 OR,所以它忽略了所有 OR 分支的 cat_id = 1 过滤器。

关于MySQL查询关系表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6862369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com