[英]Mysql - column association/join
i don't know how to explain my problem so here is an example : 我不知道如何解释我的问题,所以这是一个例子:
table products 餐桌产品
id_product / name_product
1 / aphone
2 / bphone
table tyds, product_tyd = id_product 表类型,product_tyd = id_product
id_tyd / product_tyd / user_tyd / action_tyd / data_tyd
11 / 1 / 1 / 5 / null
12 / 1 / 1 / 8 / 150
13 / 1 / 2 / 3 / null
14 / 1 / 2 / -8 / 250
15 / 2 / 3 / 2 / null
16 / 2 / 3 / 8 / 150
17 / 2 / 2 / 6 / null
18 / 2 / 2 / 8 / 250
table collections is exactly the same than table tyds but for old lines : 表集合与表类型完全相同,但对于旧行:
id_collection / product_collection / user_collection / action_collection / data_collection
1 / 1 / 9 / 5 / null
2 / 1 / 9 / -3 / null
3 / 1 / 9 / -8 / 150
So i'd like to select the lines where - action_collection = 8 OR -8 - action_tyd = 8 OR -8 with for each line the data_tyd AND/OR the data_collection value but only for a specific id_product... 所以我想选择以下行-action_collection = 8 OR -8-action_tyd = 8 OR -8,其中每一行data_tyd和/或data_collection值,但仅针对特定的id_product ...
Something like that : 像这样:
results i'm expecting for a request on an id_product = 1 : 我期望对id_product = 1的请求的结果:
id_product / user_x / action_x / data_x
1 / 1 / 8 / 150
1 / 2 / -8 / 250
1 / 9 / -8 / 150
So the point is to rely action_tyd AND data_tyd, which i do with CASE in other request and to filter lines no matter which table is in...except that is not the same column name of course... 所以关键是要依靠action_tyd和data_tyd(这是我在其他请求中使用CASE所做的),并且无论在哪个表中都要过滤行...除了当然不是相同的列名...
I hope my example is clear...I'm actually trying to make it in two separate requests. 我希望我的例子很清楚...我实际上是在尝试两个单独的请求。 One for table tyds, one for table collections and i think it can be done but one single request could do the job too...
一个用于表类型,一个用于表集合,我认为可以做到,但是一个单独的请求也可以完成这项工作...
Thanks for your help ! 谢谢你的帮助 !
I think you should combine tyds
and collections
into a single table (with a suitable column to flag from which table each record originates), but as things stand you're simply looking to perform a UNION
: 我认为您应该将
tyds
和collections
合并到一个表中(用合适的列来标记每个记录源自哪个表),但是就目前的情况而言,您只是想执行UNION
:
SELECT product_tyd AS id_product,
user_tyd AS user_x,
action_tyd AS action_x,
data_tyd AS data_x
FROM tyds
WHERE product_tyd = 1 AND action_tyd IN (-8,8)
UNION ALL
SELECT product_collection,
user_collection,
action_collection,
data_collection
FROM collections
WHERE product_collection = 1 AND action_collection IN (-8,8)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.