简体   繁体   English

MYSQL多对3个表进行查询

[英]MYSQL many to many 3 tables query

EDIT. 编辑。 I missed the one main issue I was having. 我错过了我遇到的一个主要问题。 I want to display all the unique 'device_MAC' rows. 我想显示所有唯一的'device_MAC'行。 So I want this query to output 3 rows (as per the original query). 所以我希望这个查询输出3行(根据原始查询)。 The issue I am having is connecting the data table to the remote_node table via dt_short = rn_short where the maximum timestamp for dt_short in the data table. 我有是连接问题data表到remote_node通过表dt_short = rn_short其中的最大时间戳dt_shortdata表中。

I am having trouble running a query on 3 tables (2 have many to many relations). 我在3个表上运行查询时遇到问题(2个表有多对多关系)。

What I am trying to do: 我想做什么:

  1. Get each distinct rn_IEEE from the remotenodes table with the maximum timestamp (in the example this will get 3 rows with 3 distinct short addresses rn_short ) remotenodes表中获取具有最大时间戳的每个不同的rn_IEEE (在示例中,这将获得3行,具有3个不同的短地址rn_short
  2. Join with the devicenames table on device_IEEE 加入device_IEEE上的devicenames
  3. Get each distinct dt_short from the data table with the maximum timestamp 使用最大时间戳从data表中获取每个不同的dt_short
  4. Join dt_short with rn_short from the query above 从上面的查询中加入dt_shortrn_short

Now the problem I am running into is that I can do the queries for the above individually, I have even gotten the first 3 of them together into a query but I cannot seem to properly join the last bit of data to get the result that I want. 现在我遇到的问题是我可以单独对上面的查询进行查询,我甚至将它们中的前三个一起放入查询中但我似乎无法正确加入最后一位数据以获得我的结果想。

I have been going in circles trying to solve this. 我一直在试图解决这个问题。 Here is a link to SQL Fiddle which contains all the test data and the query as far as I got it, it does what i want for the first line but from table 'data' after the first line is NULL: 这是一个指向SQL Fiddle的链接,其中包含所有测试数据和查询,就我所知,它完成了我想要的第一行,但是在第一行为NULL之后从表'data':

See this SQL fiddle 看到这个SQL小提琴

After going through your requirements and the data, it looks like you just need to change your query to include an INNER JOIN on the data table instead of a LEFT JOIN 在完成您的需求和数据之后,看起来您只需更改查询以在data表中包含INNER JOIN而不是LEFT JOIN

See SQL Fiddle with Demo 请参阅SQL Fiddle with Demo

select rn.*, dn.*, d.*
from remotenodes rn
inner join devicenames dn
  on rn.rn_IEEE = dn.device_IEEE
  and rn.rn_timestamp = (SELECT MAX(rn_timestamp) FROM remotenodes 
                              WHERE rn.rn_IEEE = rn_IEEE 
                              GROUP BY rn_IEEE)
inner join data d
  on rn.rn_short = d.dt_short
  AND d.dt_timestamp = (SELECT MAX(d2.dt_timestamp) AS ts
                        FROM data d2 
                        WHERE d.dt_short = d2.dt_short
                        GROUP BY d2.dt_short)

Try this query, for me its returning one row: 试试这个查询,对我来说它返回一行:

SELECT rn_short, rn_IEEE, device_name
FROM 
(SELECT DISTINCTROW dt_short FROM (SELECT * FROM `data` ORDER BY `dt_timestamp` DESC) as data ) as a
JOIN 
(SELECT rn_IEEE, rn_short, device_name FROM devicenames dn JOIN (SELECT DISTINCTROW rn_IEEE, rn_short FROM (SELECT * FROM `remotenodes` ORDER BY `rn_timestamp` DESC) as remotenodes GROUP BY rn_IEEE) as rn ON dn.device_IEEE = rn.rn_IEEE) as b
ON a.dt_short = b.rn_short

what you have done the query in your SQL fiddle is right.Instead of using left join use inner join so that it will give you the first row 你在SQL小提琴中做了什么查询是对的。而不是使用左连接使用内连接,这样它就会给你第一行

cheers. 干杯。

Thanks for all your answers everyone. 感谢大家的所有答案。 I managed to solve the problem by using views. 我设法通过使用视图解决问题。 It's not the most efficient way but I think it will do for now. 这不是最有效的方式,但我认为它现在会做。 Here is the SQL Fiddle link: 这是SQL Fiddle链接:

http://sqlfiddle.com/#!2/4076e/8 http://sqlfiddle.com/#!2/4076e/8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM