mysql select 来自 3 个表,其中并非所有表都包含相同行数的信息(内部联接?!)
mysql select from 3 tables where not all tables contain infos in the same amount of rows (Inner Join?!)
看来我无法理解这里和其他地方给出的一些解决方案,所以我决定提出自己的问题。
我有三个 table。像这样:
table1
id name active
123 item1 1
234 item2 0
345 item3 1 <--- not in table2!!
456 item4 1
567 item5 1
table2
id item_id instock variants deliverytimes
1 123 0 S 21days
2 123 1 M 21days
3 123 2 L 21days
4 456 1 white 10days
5 456 0 black 10days
6 234 0 yellow sold
7 456 1 green sold
8 456 0 red sold
9 567 0 big sold
table3
id item_id description
1 123 Cool Shirt
2 234 Collectors Box
3 345 Comicbook
4 456 Basecap OneSize
5 567 Cool-Mug
我尝试了从 LEFT JOIN 到 RIGHT JOIN 等的几次尝试,所有结果都以多个结果结束,这些结果在我需要的第一个 table 的 ID 中不是 DISTINCT,它们也不完整。意味着如果 table2 不包含 table1 的项目,它将不会显示在结果中。
我得到的最接近的是:
select * from table1 t1
INNER JOIN (
SELECT * FROM table2
where (deliverytimes!='sold' OR instock>0) LIMIT 1 ) t2
ON t1.id=t2.item_id,
table3 t3
where t1.active = '1'
and t1.id = t2.item_id and t3.item_id = t1.id;
最后我需要一个列表:
"id, name, description"
result (as I would like it to be)
id name description
123 item1 Cool Shirt
345 item3 Comicbook
456 item4 Basecap OneSize
确实需要满足此要求:
"item needs to be t1.active=1"
和
"if items has rows in t2 show only if one row equals (instock>0 or deliverytimes!=sold)"
和
"if item has no rows in t2 show as long as t1.active=1"
最后一个问题。当我使用内部连接和内部连接时,我从来没有得到不同的 t1.id 我仍然错过了 t2 中不存在但仍处于活动状态的行=1.
您可以尝试使用左外连接 link 表 1 和表 2(这将确保您将从表 1 中获取所有记录)。现在 link tbale3 作为内部连接,这将确保获取与 table1 相关的所有记录。
Table1 LEFT OUTER JOIN TABLE2 和 Table1 INNER JOIN table3。
以上是我的假设,你可以试试上面的逻辑
试试这个
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
如果您想在 where 子句中使用 active = 1 和 != soled filter 过滤结果
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
WHERE t1.active = 1 AND t2.deliverytimes != 'sold'
OR
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id AND t2.deliverytimes != 'sold'
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
WHERE t1.active = 1
开始编写查询时,请考虑要显示的数据。在您的情况下,您想要显示来自 table1 和 table3 的数据,因此将它们和 select 连接起来。 table2 的条件属于 WHERE 子句。
表 2 的条件是:
- t2 中不存在条目
- 或 t2 中存在库存 > 0 或交货时间 != 已售出的条目
这意味着一个 EXISTS 子句,一个 NOT EXISTS 子句,两者都与 OR 组合。
select t1.id, t1.name, t3.description
from t1
join t3 on t3.item_id = t1.item_id
where t1.active = 1
and
(
not exists
(
select *
from t2
where t2.item_id = t1.item_id
)
or
exists
(
select *
from t2
where t2.item_id = t1.item_id
and (instock > 0 or deliverytimes != 'sold')
)
);
看来我无法理解这里和其他地方给出的一些解决方案,所以我决定提出自己的问题。
我有三个 table。像这样:
table1
id name active
123 item1 1
234 item2 0
345 item3 1 <--- not in table2!!
456 item4 1
567 item5 1
table2
id item_id instock variants deliverytimes
1 123 0 S 21days
2 123 1 M 21days
3 123 2 L 21days
4 456 1 white 10days
5 456 0 black 10days
6 234 0 yellow sold
7 456 1 green sold
8 456 0 red sold
9 567 0 big sold
table3
id item_id description
1 123 Cool Shirt
2 234 Collectors Box
3 345 Comicbook
4 456 Basecap OneSize
5 567 Cool-Mug
我尝试了从 LEFT JOIN 到 RIGHT JOIN 等的几次尝试,所有结果都以多个结果结束,这些结果在我需要的第一个 table 的 ID 中不是 DISTINCT,它们也不完整。意味着如果 table2 不包含 table1 的项目,它将不会显示在结果中。
我得到的最接近的是:
select * from table1 t1
INNER JOIN (
SELECT * FROM table2
where (deliverytimes!='sold' OR instock>0) LIMIT 1 ) t2
ON t1.id=t2.item_id,
table3 t3
where t1.active = '1'
and t1.id = t2.item_id and t3.item_id = t1.id;
最后我需要一个列表:
"id, name, description"
result (as I would like it to be)
id name description
123 item1 Cool Shirt
345 item3 Comicbook
456 item4 Basecap OneSize
确实需要满足此要求:
"item needs to be t1.active=1" 和 "if items has rows in t2 show only if one row equals (instock>0 or deliverytimes!=sold)" 和 "if item has no rows in t2 show as long as t1.active=1"
最后一个问题。当我使用内部连接和内部连接时,我从来没有得到不同的 t1.id 我仍然错过了 t2 中不存在但仍处于活动状态的行=1.
您可以尝试使用左外连接 link 表 1 和表 2(这将确保您将从表 1 中获取所有记录)。现在 link tbale3 作为内部连接,这将确保获取与 table1 相关的所有记录。
Table1 LEFT OUTER JOIN TABLE2 和 Table1 INNER JOIN table3。
以上是我的假设,你可以试试上面的逻辑
试试这个
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
如果您想在 where 子句中使用 active = 1 和 != soled filter 过滤结果
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
WHERE t1.active = 1 AND t2.deliverytimes != 'sold'
OR
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id AND t2.deliverytimes != 'sold'
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
WHERE t1.active = 1
开始编写查询时,请考虑要显示的数据。在您的情况下,您想要显示来自 table1 和 table3 的数据,因此将它们和 select 连接起来。 table2 的条件属于 WHERE 子句。
表 2 的条件是:
- t2 中不存在条目
- 或 t2 中存在库存 > 0 或交货时间 != 已售出的条目
这意味着一个 EXISTS 子句,一个 NOT EXISTS 子句,两者都与 OR 组合。
select t1.id, t1.name, t3.description
from t1
join t3 on t3.item_id = t1.item_id
where t1.active = 1
and
(
not exists
(
select *
from t2
where t2.item_id = t1.item_id
)
or
exists
(
select *
from t2
where t2.item_id = t1.item_id
and (instock > 0 or deliverytimes != 'sold')
)
);