Skip to main content
Tweeted twitter.com/StackDrupal/status/1530338219447005184
improved formatting and spelling
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Is it Possiblepossible to have a nested Select StatementSELECT in a WhereWHERE clause using Dynamic Queriesdynamic queries?

In my Drupal Query I need to recreate something likeexecute this SQL query using a dynamic query:queries.

WHERE (node_field_data.status = 1) 
AND (node__field_document_places.entity_id IN (SELECT entity_id FROM node__field_document_places where field_document_places_target_id = 51673)) 
WHERE (node_field_data.status = 1) 
AND (node__field_document_places.entity_id IN (SELECT entity_id FROM node__field_document_places where field_document_places_target_id = 51673)) 

In my code hereThis is the relevant part of my dynamic querycode I am building:using.

$connection = Database::getConnection();
$query = $connection->select('node_field_data', 'node_field_data');

$query->condition('node_field_data.status',1);
$query->condition('node__field_document_places.entity_id',[ -- Can I do this here somehow? -- ], "IN");
$connection = Database::getConnection();
$query = $connection->select('node_field_data', 'node_field_data');

$query->condition('node_field_data.status',1);
$query->condition('node__field_document_places.entity_id',[ -- Can I do this here somehow? -- ], "IN");

I could technically run a seperate Query before handseparate query and outputput the idsIDs in an array here. But I was hoping to keep a single DB call to help with speedavoid executing two different queries.

My Question is, isIs there a way to do this using dynamic queries, and is there even a performance benefit to do it this way.?

Is it Possible to have a nested Select Statement in a Where clause using Dynamic Queries

In my Drupal Query I need to recreate something like this using a dynamic query:

WHERE (node_field_data.status = 1) 
AND (node__field_document_places.entity_id IN (SELECT entity_id FROM node__field_document_places where field_document_places_target_id = 51673)) 

In my code here is the relevant part of my dynamic query I am building:

$connection = Database::getConnection();
$query = $connection->select('node_field_data', 'node_field_data');

$query->condition('node_field_data.status',1);
$query->condition('node__field_document_places.entity_id',[ -- Can I do this here somehow? -- ], "IN");

I could technically run a seperate Query before hand and output the ids in array here. But I was hoping to keep a single DB call to help with speed.

My Question is, is there a way to do this using dynamic queries, and is there even a performance benefit to do it this way.

Is it possible to have a nested SELECT in a WHERE clause using dynamic queries?

I need to execute this SQL query using dynamic queries.

WHERE (node_field_data.status = 1) 
AND (node__field_document_places.entity_id IN (SELECT entity_id FROM node__field_document_places where field_document_places_target_id = 51673)) 

This is the code I am using.

$connection = Database::getConnection();
$query = $connection->select('node_field_data', 'node_field_data');

$query->condition('node_field_data.status',1);
$query->condition('node__field_document_places.entity_id',[ -- Can I do this here somehow? -- ], "IN");

I could technically run a separate query and put the IDs in an array. But I was hoping to avoid executing two different queries.

Is there a way to do this using dynamic queries?

Became Hot Network Question
Source Link

Is it Possible to have a nested Select Statement in a Where clause using Dynamic Queries

In my Drupal Query I need to recreate something like this using a dynamic query:

WHERE (node_field_data.status = 1) 
AND (node__field_document_places.entity_id IN (SELECT entity_id FROM node__field_document_places where field_document_places_target_id = 51673)) 

In my code here is the relevant part of my dynamic query I am building:

$connection = Database::getConnection();
$query = $connection->select('node_field_data', 'node_field_data');

$query->condition('node_field_data.status',1);
$query->condition('node__field_document_places.entity_id',[ -- Can I do this here somehow? -- ], "IN");

I could technically run a seperate Query before hand and output the ids in array here. But I was hoping to keep a single DB call to help with speed.

My Question is, is there a way to do this using dynamic queries, and is there even a performance benefit to do it this way.