[英]In mysql how do I add another table join within a subquery?
Table: Unit
ID NAME VALUE
1 Kilo 1000
2 Mega 1000000
3 Giga 1000000000
Table: Storage
ID Title Drive_value DriveUnitID Cache_value CacheUnitID Status_ID error error_unit area
1 Seagate 100 3 400 1 2 1 1 1
2 Scansoft 250 3 80 2 1 1 2 2
Table: manufac
ID area
1 US
2 CHINA
Table: Status
ID Description
1 Blah.. Blah
2 Durka Durka
Desired goal is to have a subselect joining three tables Something like this (below) but this syntax doesnt work 期望的目标是让子选择连接三个表(如下),但是此语法不起作用
select
s.title,
t.description,
x.area,
u1.value * s.cache_value as Cache,
u2.value * s.drive_value as Drive,
u3.value * s.error_value as Error
((u4.value * s.error_value)+(u4.value * s.error_value)) as ErrorHigh
((u5.value * s.error_value)-(u5.value * s.error_value)) as ErrorHigh
from storage s
join status t on t.id = s.status_id
join manufac x on x.id = s.manufac_id
join unit u1 on s.cache_unit_id = u1.id
join unit u2 on s.drive_unit_id = u2.id
join unit u3 on s.error_unit_id = u3.id
join unit u4 on s.error_unit_id = u4.id
join unit u5 on s.error_unit_id = u5.id
check your query, the third inner join has no condition. 检查您的查询,第三个内部联接没有条件。 And you don't need to include Status
in your FROM
clause if you're going to INNER JOIN
it! 而且,如果您要INNER JOIN
则无需在您的FROM
子句中包含Status
!
SELECT
s.Title,
t.Description,
u1.VALUE * s.Drive_value AS Drive,
u2.VALUE * s.Cache_value AS Cache
FROM Storage s
INNER JOIN Unit u1 ON u1.ID = s.DriveUnitID
INNER JOIN Unit u2 ON u2.ID = s.CacheUnitID
INNER JOIN Status t ON t.ID = s.Status_ID;
This queries are semantically the same. 这些查询在语义上是相同的。 But remember either you put the table in the join or in the from clause: 但是请记住,您将表放在了join或from子句中:
With explicit joins: 使用显式联接:
select s.title, t.description,
u1.value * s.cache_value as Cache,
u2.value * s.drive_value as Drive
from storage s
join status t on t.id = s.status_id
join unit u1 on s.cache_unit_id = u1.id
join unit u2 on s.drive_unit_id = u2.id
With implicit joins: 使用隐式联接:
select s.title, t.description,
u1.value * s.cache_value as Cache,
u2.value * s.drive_value as Drive
from storage s, status t, unit u1, unit u2
where t.id = s.status_id and s.cache_unit_id = u1.id and s.drive_unit_id = u2.id
The final result is the same for the end user and the DBMS also process both query the same way, so there are no performance increment by using one or the other. 对于最终用户而言,最终结果是相同的,并且DBMS还以相同的方式处理这两个查询,因此使用一个或另一个不会提高性能。
Edit: 编辑:
After the requirements changed this is the query I think you're looking for: 需求更改后,这就是我想查询的查询:
select s.title, t.description,
u1.value * s.cache_value as Cache,
u2.value * s.drive_value as Drive,
u3.value * s.error_value as Error,
u3.value * s.error_value * 2 as ErrorHigh,
u3.value * s.error_value - u3.value * s.error_value as ErrorLow,
m.area
from storage s
join status t on t.id = s.status_id
join manufac m on m.id = s.area
join unit u1 on s.cache_unit_id = u1.id
join unit u2 on s.drive_unit_id = u2.id
join unit u3 on s.error_unit_id = u3.id
Result: 结果:
+----------+-------------+----------+--------------+---------+-----------+----------+-------+
| TITLE | DESCRIPTION | CACHE | DRIVE | ERROR | ERRORHIGH | ERRORLOW | AREA |
+----------+-------------+----------+--------------+---------+-----------+----------+-------+
| Seagate | Durka Durka | 400000 | 100000000000 | 1000 | 2000 | 0 | US |
| Scansoft | Blah.. Blah | 80000000 | 250000000000 | 1000000 | 2000000 | 0 | CHINA |
+----------+-------------+----------+--------------+---------+-----------+----------+-------+
So I guess this query is not what you really need, but should be enough to lead you in the right direction. 所以我想这个查询不是您真正需要的,但足以引导您朝正确的方向前进。 Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.