简体   繁体   English

MySQL内部连接/左连接

[英]MySQL Inner join / Left join

I have the following MySQL statement 我有以下MySQL语句

SELECT * FROM node
        LEFT JOIN field_data_field_featured ON field_data_field_featured.entity_id = node.nid 
        LEFT JOIN field_data_field_mobile_only ON field_data_field_mobile_only.entity_id = node.nid AND field_data_field_mobile_only.field_mobile_only_value = 1
        INNER JOIN field_data_field_related_show ON field_data_field_related_show.entity_id = node.nid 
        WHERE node.status = '1' AND (node.type IN ('game','video','post')) 
        AND field_related_show_nid = 64
        ORDER BY field_featured_value DESC, node.created DESC

It currently returns all records that fit the conditions stated but I would like to add a conditional statement where if field type is a game I would only like to include the record in the result set if the field field_mobile_only_value is 1. 当前它返回所有符合所述条件的记录,但是我想添加一个条件语句,如果字段类型是游戏,那么我只想在字段field_mobile_only_value为1的情况下将记录包括在结果集中。

A sample record of the current SQL looks like this 当前SQL的样本记录如下所示

      nid: 351
                    vid: 351
                   type: game
               language: und
                  title: Avalanche at Plankton's Peak
                    uid: 53
                 status: 1
                created: 1403730218
                changed: 1403730218
                comment: 0
                promote: 1
                 sticky: 0
                   tnid: 0
              translate: 0
            entity_type: node
                 bundle: game
                deleted: 0
              entity_id: 351
            revision_id: 351
               language: und
                  delta: 0
   field_featured_value: 0
            entity_type: NULL
                 bundle: NULL
                deleted: NULL
              entity_id: NULL
            revision_id: NULL
               language: NULL
                  delta: NULL
field_mobile_only_value: NULL
            entity_type: node
                 bundle: game
                deleted: 0
              entity_id: 351
            revision_id: 351
               language: und
                  delta: 0
 field_related_show_nid: 64

I don't think you can add a column to the result based on the value of another column in your select query. 我认为您无法根据选择查询中另一列的值向结果添加一列。

Why don't you verify this in your php code? 为什么不在您的php代码中对此进行验证?

If it is not a game, just ignore the field_mobile_only_value column. 如果不是游戏,则忽略field_mobile_only_value列。

You could add something like this to the WHERE clause: 您可以在WHERE子句中添加以下内容:

AND ( ( node.type = 'game' 
      AND field_data_field_mobile_only.field_mobile_only_value = 1 
      )
    OR node.type <> 'game'
    )

(We can use the inequality operator here, without a concern for NULL values, because a previous predicate has already guaranteed that the values of node.type will not be NULL.) (我们可以在此处使用inequality运算符,而无需考虑NULL值,因为先前的谓词已经保证node.type的值不会为NULL。)

If type is 'game' , then we only return the row if is equal to 1 . 如果type'game' ,则仅返回等于1的行。

Otherwise, if type is something other than game , we allow the row to be returned. 否则,如果typegame以外的其他type ,我们允许返回该行。

In the more general case, where we would want to also return rows that had NULL values for type , a slight modification using the null-safe comparator: 在更一般的情况下,我们还希望返回typeNULL行,使用null安全比较器进行一些修改:

AND ( ( node.type = 'game'
      AND field_data_field_mobile_only.field_mobile_only_value = 1
      )
    OR NOT ( node.type <=> 'game' )
    )

would give an equivalent result. 会给出等效的结果。

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

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