简体   繁体   English

如何通过带有where子句的共享列ID联接同一表中的行?

[英]How to join rows in the same table by a shared column id with a where clause?

I'm trying to write a SQL query that looks at a single MySQL DB table ( exp_playa_relationships ) which stores relationship data of CMS' posts and which data structure looks like this: 我正在尝试编写一个SQL查询,该查询查看单个MySQL数据库表( exp_playa_relationships ),该表存储CMS帖子的关系数据,并且该数据结构如下所示:

rel_id  |  parent_entry_id  |  parent_field_id  |  child_entry_id
-----------------------------------------------------------------
55      |  3                |  2                |  1
56      |  3                |  2                |  4
58      |  1                |  2                |  4
59      |  8                |  4                |  2
60      |  8                |  5                |  1
63      |  4                |  2                |  3
64      |  9                |  4                |  6
65      |  9                |  5                |  3

rel_id is unique, other columns are not. rel_id是唯一的,其他列则不是。

I would like to generate the following out of the data above: 我想从上面的数据中生成以下内容:

event_data_id  |  user_id  |  event_id
--------------------------------------
8              |  1        |  2
9              |  3        |  6

The parent_field_id value itself is discarded in the final output but is needed to figure out if the row's child_entry_id signifies a user_id or event_id . parent_field_id值本身在最终输出中被丢弃,但是需要弄清该行的child_entry_id表示user_idevent_id

parent_entry_id is the event_data_id . parent_entry_idevent_data_id

So in plain english I would like to: 因此,我想用简单的英语说:

  1. Filter rows that have a parent_field_id value of either 4 or 5 过滤parent_field_id值为45
  2. Out of those rows, I want to join all those that share the same parent_entry_id . 在这些行中,我想加入所有共享相同parent_entry_id
  3. Return the parent_entry_id as event_data_id . 返回parent_entry_id作为event_data_id
  4. Return the child_entry_id as a user_id if parent_field_id of the same row is 5 . 如果同一行的parent_field_id5child_entry_id作为user_id返回。
  5. Return the child_entry_id as a event_id if parent_field_id of the same row is 4 . 如果同一行的parent_field_id4child_entry_id作为event_id返回。

My current SQL query (not working) is this: 我当前的SQL查询(不起作用)是这样的:

SELECT
    t1.`parent_entry_id` AS event_data_id,
    t1.`child_entry_id` AS user_id,
    t1.`child_entry_id` AS event_id

FROM `exp_playa_relationships` AS t1
INNER JOIN `exp_playa_relationships` AS t2
ON t1.`parent_entry_id` = t2.`parent_entry_id`
WHERE t1.`parent_field_id` = 4 OR t1.`parent_field_id` = 5

What I cannot figure out specifically is how to avoid creating duplicates on the parent_entry_id (SQL creates 2 new rows per row) and how to return child_entry_id as either user_id or event_id based on the parent_field_id value. 我不能具体弄清楚的是如何避免在parent_entry_id上创建重复项(SQL每行创建2个新行),以及如何基于parent_field_id值将child_entry_id作为user_idevent_id返回。

Any ideas would be much appreciated. 任何想法将不胜感激。

You're sooo close: SOOO接近:

SELECT t1.`parent_entry_id` AS event_data_id,
       t1.`child_entry_id` AS user_id,
       t2.`child_entry_id` AS event_id
FROM `exp_playa_relationships` AS t1
INNER JOIN `exp_playa_relationships` AS t2
        ON t2.`parent_entry_id` = t1.`parent_entry_id`
           AND t2.`parent_field_id` = 4
WHERE  t1.`parent_field_id` = 5

Specifically, you're having to tell it which row-set to pull the relevant data from. 具体来说,您必须告诉它从哪个行集中提取相关数据。

By the way, your current database design will cause you more of these types of headaches... I'd recommend pulling the information out into 'result' tables (unless that's what this is for?). 顺便说一句,您当前的数据库设计将使您更加头痛……我建议您将信息提取到“结果”表中(除非这是为了什么?)。

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

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