Skip to main content
removed "Drupal" from the title; removed tags from the title; added an explicit question
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Writing a LEFT JOIN in Drupal 8

I have the following raw SQL:

 SELECT *
 FROM node_field_data n
 LEFT JOIN book b ON n.nid=b.nid
 WHERE n.type IN ('book')
 AND (b.has_children != 1 OR b.nid IS NULL)

I have a module that I need to add this query to, so I already have a $query object. The problem I'm having is finding the documentation/syntax to do the LEFT JOIN part of my query.

This is what I've added to $query$query.

 $query->addWhere(0,'node.nid', 'b.nid');
 $query->addWhere(0,$types,'IN');
 $query->addWhere(0,'b.has_children',1,'<>');
 $query->addWhere(1,'b.nid','IS NULL');

How do I add the LEFT JOIN part?

Writing a LEFT JOIN in Drupal 8

I have the following raw SQL:

 SELECT *
 FROM node_field_data n
 LEFT JOIN book b ON n.nid=b.nid
 WHERE n.type IN ('book')
 AND (b.has_children != 1 OR b.nid IS NULL)

I have a module that I need to add this query to, so I already have a $query object. The problem I'm having is finding the documentation/syntax to do the LEFT JOIN part of my query.

This is what I've added to $query

 $query->addWhere(0,'node.nid', 'b.nid');
 $query->addWhere(0,$types,'IN');
 $query->addWhere(0,'b.has_children',1,'<>');
 $query->addWhere(1,'b.nid','IS NULL');

Writing a LEFT JOIN

I have the following raw SQL:

 SELECT *
 FROM node_field_data n
 LEFT JOIN book b ON n.nid=b.nid
 WHERE n.type IN ('book')
 AND (b.has_children != 1 OR b.nid IS NULL)

I have a module that I need to add this query to, so I already have a $query object. The problem I'm having is finding the documentation/syntax to do the LEFT JOIN part of my query.

This is what I've added to $query.

 $query->addWhere(0,'node.nid', 'b.nid');
 $query->addWhere(0,$types,'IN');
 $query->addWhere(0,'b.has_children',1,'<>');
 $query->addWhere(1,'b.nid','IS NULL');

How do I add the LEFT JOIN part?

Source Link
StevenPatz
  • 586
  • 6
  • 21

Writing a LEFT JOIN in Drupal 8

I have the following raw SQL:

 SELECT *
 FROM node_field_data n
 LEFT JOIN book b ON n.nid=b.nid
 WHERE n.type IN ('book')
 AND (b.has_children != 1 OR b.nid IS NULL)

I have a module that I need to add this query to, so I already have a $query object. The problem I'm having is finding the documentation/syntax to do the LEFT JOIN part of my query.

This is what I've added to $query

 $query->addWhere(0,'node.nid', 'b.nid');
 $query->addWhere(0,$types,'IN');
 $query->addWhere(0,'b.has_children',1,'<>');
 $query->addWhere(1,'b.nid','IS NULL');