简体   繁体   English

离开联接或联盟的MySQL查询?

[英]left join or union for mysql query?

I'm struggling with a query, I've tried UNION but it needs the same number of column results so it's no good, here's what I'm trying to do: 我在查询中苦苦挣扎,我尝试了UNION,但是它需要相同数量的列结果,所以这不好,这就是我想要做的事情:

I'm trying to select the latest rows from table actions: 我正在尝试从表操作中选择最新的行:

 action_id INT  |  media_id INT |   badge_id INT  | performed_at TS
     ______________________________________________________________
        1       |        365    |     NULL        | timestamp
        2       |       NULL    |        26       | timestamp
        3       |       62      |       NULL      | timestamp

If media_id isn't NULL, I want to get corresponding row from the table title and if badge_id isn't NULL I want to get the corresponding row from the table badge. 如果media_id不为NULL,我想从表标题中获取对应的行,如果badge_id不为NULL,我想从表中获取对应的行。

So I tried this (but it doesn't work because title and badges do not have the same number of columns): 因此,我尝试了此操作(但由于标题和徽章的列数不同,因此无法正常工作):

 (SELECT * FROM actions, actions_type, a3m_account, title 
            WHERE actions.type_id = actions_type.type_id 
            AND actions.performed_by_user_id = a3m_account.id 
            AND actions.media_id = title.media_id)

            . ' UNION '

            (SELECT * FROM actions, actions_type, a3m_account, badges 
            WHERE actions.type_id = actions_type.type_id
            AND actions.performed_by_user_id = a3m_account.id 
            AND actions.badge_id = badges.badge_id) 


            . 'ORDER BY performed_at DESC '
            . 'LIMIT 10';

Edit: I'm basically creating a twitter style feed with all the actions that were taken on the site. 编辑:我基本上是创建一个Twitter样式供稿,其中包含在网站上执行的所有操作。 Some of those actions are related to a media (table title) and others are related to a badge (table badge). 其中一些动作与媒体(表格标题)有关,而其他动作与徽章(表格徽章)有关。 I want to get those last actions and their related data (the issue being that the related data has to come from different tables which have different columns). 我想获取这些最后的动作及其相关数据(问题是相关数据必须来自具有不同列的不同表)。

How can I do this? 我怎样才能做到这一点?

Since UNION only works on tables with the same schema, the query that you have to write has the following structure (you have not specified the fields you want): 由于UNION仅适用于具有相同模式的表,因此您必须编写的查询具有以下结构(您尚未指定所需的字段):

 (SELECT <1st field list> 
 FROM actions, actions_type, a3m_account, title
 WHERE actions.type_id = actions_type.type_id 
 AND actions.performed_by_user_id = a3m_account.id 
 AND actions.media_id = title.media_id)

 UNION

 (SELECT <2nd fields list>
 FROM actions, actions_type, a3m_account
 WHERE actions.type_id = actions_type.type_id
 AND actions.performed_by_user_id = a3m_account.id 
 AND actions.badge_id = badges.badge_id) 

In <1st fields list> and <2nd fields list>, you must have the same number of fields and same type in both field lists. 在<第一字段列表>和<第二字段列表>中,两个字段列表中的字段数和类型必须相同。 If needed, you can use the AS clause so that you have same field names in both lists. 如果需要,可以使用AS子句,以便两个列表中的字段名称相同。

Example: 例:

SELECT actions.action_id, actions.media_id AS additional_info
FROM ...
UNION 
SELECT actions.action_id, actions.badge_id AS additional_info
FROM ...

In order of importance: 按重要性顺序:

  1. Rather than select * from all tables, select * from the common tables and specifically select columns from title and badge such that the number of columns selected from those tables is the same and have the same type 而不是选择*从所有的表中,选择*从共同的表和选择具体的列titlebadge ,使得列从这些表中选择的数量是相同的,并具有相同类型的
  2. And where media_id is not null to the first query and where badge_id is not null to the second query. 并且where media_id is not null对第一个查询where media_id is not nullwhere badge_id is not null对第二个查询where badge_id is not null Note that if both id's are present, you'll get two rows for the item 请注意,如果两个ID都存在,则该项目将获得两行
  3. Remove the brackets from sound your selects (so they are not inner queries, but just part of the one query 从您选择的声音中删除方括号(因此它们不是内部查询,而只是一个查询的一部分
  4. Use proper join syntax (ie use the join keyword) 使用正确的联接语法(即,使用join关键字)

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

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