[英]Multiple inner joins with multiple tables
So I have four tables. 所以我有四张桌子。 Each table has a single id for the previous table id. 每个表都有一个前一个表id的id。 So my in click table has an id and an id for the ad from which it came. 所以我在点击表中有一个ID和一个ID来自它的广告。 In the ad table, it has an id for the ad and one for the campaign it's from. 在广告表中,它具有广告ID和来自广告系列的ID。 So here's an example. 所以这是一个例子。
Table4 -
id company table_id
11 hp 20
12 apple 23
13 kohls 26
14 target 21
15 borders 28
Table3 -
id value table2_id
21 ks 53
22 al 54
23 tx 53
24 fl 55
25 co 51
Table2 -
id value table1_id
51 ks 34
52 al 34
53 tx 33
54 fl 35
55 co 31
Table1 -
id value
31 ks
32 al
33 tx
34 fl
35 co
So to find out where the values in Table 4 came from, I need to work back through each table and check which id they have. 因此,为了找出表4中的值来自何处,我需要回顾每个表并检查它们具有哪个ID。 Basically, I want to know which values in table 1 are associated with the values in table 4. 基本上,我想知道表1中的哪些值与表4中的值相关联。
This of table 4 as visitors to a website and Table 1 as internet ads. 表4作为网站访问者和表1作为互联网广告。 I want to know which visitors came from which ads. 我想知道哪些访问者来自哪些广告。 Unfortunately, the data is set up so that I can only take single steps back from visitor to source to ad group to ad. 不幸的是,数据已设置好,因此我只能从访问者到源代码,广告组再到广告。 Does that make sense? 那有意义吗?
Anyways, I'm wondering if using 4 innner joins was the optimal strategy for this problem or is there some simpler mysql solution that i'm not aware of. 无论如何,我想知道是否使用4个内部连接是这个问题的最佳策略,还是有一些我不知道的更简单的mysql解决方案。
Inner joins are probably the best method, and you only need 3. 内连接可能是最好的方法,你只需要3。
This will give you a result set with two columns: company and associated values. 这将为您提供包含两列的结果集:公司和关联值。
SELECT Table4.company, table1.id, table1.value
FROM Table1
INNER JOIN Table2
ON Table2.table1_id = Table1.id
INNER JOIN Table3
ON Table3.table2_id = Table2.id
INNER JOIN Table4
ON Table4.table3_id = Table3.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.