将一个表中的信息连接到另外两个表的 UNION 上...这可能吗?
我想要做的是这样的:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据您描述的所需结果,我认为您实际上正在寻找
OUTER JOIN
。试试这个:此查询将返回
cat_index1
和cat_index2
中的所有产品及其关联记录。如果cat_index1
或cat_index2
中不存在匹配记录,结果集中仍将包含产品行。Based on the results you described you desire, I think you're actually looking for an
OUTER JOIN
. Try this:This query will return all the products and their associated records in
cat_index1
andcat_index2
. If no matching record exists incat_index1
orcat_index2
, you'll still have the products row included in your result set.尝试使用
Try to use
尝试使用 WHERE IN () 和 union cat_index1.cid 和 cat_index_2.cid:
Try using WHERE IN () and union cat_index1.cid and cat_index_2.cid: