将一个表中的信息连接到另外两个表的 UNION 上...这可能吗?

发布于 2024-11-30 12:17:11 字数 499 浏览 6 评论 0原文

我想要做的是这样的:

SELECT *  
FROM products as p, cat_index1 as c1, cat_index_2 as c2  
WHERE p.pid = c1.cid OR p.pid = c2.cid

并不是我期望它能工作,但是 EXPLAIN 会产生以下错误:

Impossible WHERE noticed after reading const table...

我有一个产品列表和每个类别的表格,我想从更多信息中获取信息一次不止一个类别,并将其与产品信息一起加入。表中的产品可以属于两个类别、一个类别或两者都不属于。输出需要是 cat_index1 与 cat_index2 的并集,我想将产品的信息与所述并集连接起来。

产品表相当大,里面有很多信息,但类别表是一列,只包含所述类别成员的产品 ID,当然,它等于产品表中的 id 列。当前未设置外键。

有人有什么想法吗?

What I'm looking to do is something like this:

SELECT *  
FROM products as p, cat_index1 as c1, cat_index_2 as c2  
WHERE p.pid = c1.cid OR p.pid = c2.cid

not that I expected it to work, but EXPLAIN yields the following error:

Impossible WHERE noticed after reading const table...

I've got a list of products and a table for each category, I would like to pull info from more than one category at a time and join it with the product info. Products in the table products can be in both categories, one category or neither. The output needs to be a union of cat_index1 with cat_index2 and I want to join the info from products with said union.

The product table is pretty big, lots of info in there, but the category tables are a single column just containing the product ids of members of said category which are, of course, equal to the id column in the product table. No foreign keys are currently set.

Anyone have any ideas?

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

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

发布评论

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

评论(4

最近可好 2024-12-07 12:17:11
SELECT *  
FROM products as p
inner join cat_index1 as c1 on (p.pid = c1.cid)

union

SELECT *  
FROM products as p
inner join cat_index2 as c2 on (p.pid = c2.cid)
SELECT *  
FROM products as p
inner join cat_index1 as c1 on (p.pid = c1.cid)

union

SELECT *  
FROM products as p
inner join cat_index2 as c2 on (p.pid = c2.cid)
苍风燃霜 2024-12-07 12:17:11

根据您描述的所需结果,我认为您实际上正在寻找OUTER JOIN。试试这个:

SELECT * FROM products
    LEFT OUTER JOIN cat_index1 as c1 ON c1.cid = p.pid
    LEFT OUTER JOIN cat_index2 as c2 ON c2.cid = p.pid

此查询将返回 cat_index1cat_index2 中的所有产品及其关联记录。如果 cat_index1cat_index2 中不存在匹配记录,结果集中仍将包含产品行。

Based on the results you described you desire, I think you're actually looking for an OUTER JOIN. Try this:

SELECT * FROM products
    LEFT OUTER JOIN cat_index1 as c1 ON c1.cid = p.pid
    LEFT OUTER JOIN cat_index2 as c2 ON c2.cid = p.pid

This query will return all the products and their associated records in cat_index1 and cat_index2. If no matching record exists in cat_index1 or cat_index2, you'll still have the products row included in your result set.

捶死心动 2024-12-07 12:17:11

尝试使用

SELECT *  
FROM products as p
where 
EXISTS(select * from cat_index1 as c1 where p.pid = c1.cid) or  EXISTS(select * cat_index_2 as c2  
WHERE p.pid = c2.cid)

Try to use

SELECT *  
FROM products as p
where 
EXISTS(select * from cat_index1 as c1 where p.pid = c1.cid) or  EXISTS(select * cat_index_2 as c2  
WHERE p.pid = c2.cid)
感悟人生的甜 2024-12-07 12:17:11

尝试使用 WHERE IN () 和 union cat_index1.cid 和 cat_index_2.cid:

SELECT * FROM Products AS P WHERE P.pid IN
(
SELECT cat_index1.cid
 UNION 
SELECT cat_index_2.cid
)

Try using WHERE IN () and union cat_index1.cid and cat_index_2.cid:

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