自我加入多个条件

时间:2016-06-13 22:26:07

标签: mysql join inner-join self-join

我使用mysql存储我的化学分析。并使用html / php过滤结果并生成查询以获取我想要的结果。现在事情变得越来越复杂,我试图自己加入表格来应用所有过滤器。

我的桌面设计就像超过5万行。

+--------+---------+-------------+----------------+
| Column |  Type   |    Index    |                |
+--------+---------+-------------+----------------+
| RID    | int     | primary_key | auto increment |
| ID     | int     | index       |                |
| Type   | int     |             |                |
| Order  | int     |             |                |
| Num    | int     |             |                |
| Val1   | decimal |             |                |
| Val2   | decimal |             |                |
+--------+---------+-------------+----------------+

每个样本都有 ID ,不同的类型订单 Val1 Val2 Num 类型分析的结果。现在有42种不同的数字。

例如,如果我的过滤器是这样的,

Select Types (1,3,9)
Select ANum (0,5)
Type 1, Num 5 should be bigger than 10 In Val2
Type 1, Num 5, should be smaller than 30 In Val2
Type 3, Num 0, should be smaller than 8 In Val1

我正在使用该查询来产生结果。

SELECT analyse.* FROM analyse 
INNER JOIN 
(SELECT ID FROM analyse WHERE Type = 1 AND Num = 5 AND Val2 < 30) AS a1 ON a1.SID = analyse.SID 
INNER JOIN 
(SELECT SID FROM  WHERE Type = 1 AND Num = 5 AND Val2 > 10) AS a2 ON a2.SID = analyse.SID
INNER JOIN 
(SELECT SID FROM analyse WHERE Type = 3 AND Num = 0 AND Val1 > 8) AS a3 ON a2.SID = analyse.SID 
WHERE Type IN (1,3,9) AND Num IN (0,5) ORDER BY ID, Type, Order, Num ASC

+-----+------+-------+---------------+--------------+--------------+--------------+
| ID  | Type | Order | Val1[Num[0]]  | Val2[Num[0]] | Val1[Num[5]] | Val2[Num[5]] |
+-----+------+-------+---------------+--------------+--------------+--------------+
| ... | ...  | ...   | ...           | ...          | ...          | ...          |
| 118 | 1    | 1     | 10.9000       | 2.2083       | 3.5056       | 15.2627      |
| 118 | 1    | 2     | 9.5000        | 1.9246       | 2.0305       | 11.7049      |
| 118 | 1    | 3     | 7.9000        | 1.6005       | 2.4274       | 16.6597      |
| 118 | 2    | 1     | 10.9000       | 2.2083       | 3.5056       | 15.2627      |
| 118 | 3    | 2     | 20.4000       | 4.1329       | 2.8187       | 22.9676      |
| 118 | 4    | 3     | 28.3000       | 5.7334       | 2.7094       | 29.6273      |
| 119 | 1    | 1     | 27.2000       | 6.8635       | 0.5506       | 14.9084      |
| 119 | 1    | 2     | 25.9000       | 6.5355       | 0.4249       | 10.9550      |
| 119 | 3    | 1     | 27.2000       | 6.8635       | 0.5506       | 14.9084      |
| 119 | 3    | 2     | 53.1000       | 13.3989      | 0.4893       | 25.8634      |
| ... | ...  | ...   | ...           | ...          | ...          | ...          |
+-----+------+-------+---------------+--------------+--------------+--------------+

期望的输出应该是这样的。但是,由于还有其他Val1和Val2值具有相同的ID,相同的类型但订单不同,因此INNER JOIN不能按预期工作。总是有比我的过滤器更大和更小的值。其中一些人从未表现过。

抱歉,如果时间太长了。我无法找到正确的陈述或结构。有什么建议吗?

编辑:抱歉。我忘了提及所有其他必须显示ID的数据。这就是我尝试使用加入的原因。

2 个答案:

答案 0 :(得分:0)

所以,你对表的要求是

Select Types (1,3,9)
Select Num (0,5)
Type 1, Num 5 should be bigger than 10 In Val2
Type 1, Num 5, should be smaller than 30 In Val2
Type 3, Num 0, should be smaller than 8 In Val1

基本上需要评估4种不同的标准(Type,num,Val1,Val2)

虽然可能会有更好的&#34;解决方案(在性能方面或更易于阅读的代码)最简单的方法是创建不同的条件对,除非您处理数百万条记录,否则这些条件应该没有问题。

根据您的要求,我会想要获取以下要获取的行集:

1.) Type = 1, Num = 5, Val2 > 10
2.) Type = 1, Num = 5, Val2 < 30
3.) Type = 3, Num = 0, Val1 < 8
4.) Type = 9

所以,这将是&#34; 4明确&#34;的结合。查询:

SELECT * FROM analyse WHERE Type = 1 AND Num = 5 AND Val2 > 10
UNION
SELECT * FROM analyse WHERE Type = 1 AND Num = 5 AND Val2 < 10
UNION
SELECT * FROM analyse WHERE Type = 3 AND Num = 0 AND Val1 < 8
UNION
SELECT * FROM analyse WHERE Type = 9

要按ID过滤最终结果,您只需使用:

SELECT * FROM (
  SELECT * FROM analyse WHERE Type = 1 AND Num = 5 AND Val2 > 10
  UNION
  SELECT * FROM analyse WHERE Type = 1 AND Num = 5 AND Val2 < 10
  UNION
  SELECT * FROM analyse WHERE Type = 3 AND Num = 0 AND Val1 < 8
  UNION
  SELECT * FROM analyse WHERE Type = 9
) as tempTable WHERE id = 118
  • 或者只是将id附加到每个独立查询。

答案 1 :(得分:0)

我已经解决了我的问题,一起查询相同的Nums和Types,而不是使用单独的连接来获取它们。通过这种方式,相同的Num和Type的其他数据都不能通过过滤器。

对于那些条件

server {
    listen   80;
    server_name radio.com;
    ....
    location /stream { 
       proxy_set_header X-Forwarded-For $remote_addr; 
       proxy_pass http://stream.radio.com:8000/mount; 
    }
    ....
}

查询应该是这个,

<VirtualHost *:80>
   ServerName radio.com
   ....
   ProxyPass /stream http://stream.radio.com:8000/mount
</VirtualHost>