Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By kevinnivek on
Hello,
I'm attempting to select columns across two tables in Drupal. My code is currently the following :
$query = db_select('field_data_field_table1', 'field_data_field_table2');
$query
->fields('field_data_table1', array('entity_id'))
->fields('field_data_table2', array('entity_id'));
$query->execute();
The above code produces the following SQL :
SELECT field_data_field_table1.entity_id AS entity_id, field_data_field_table2.entity_id AS field_data_field_table2_entity_id FROM {field_data_field_table1} field_data_field_table2
Which produces the following SQL error :
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_data_field_table1.entity_id' in 'field list'
Am I missing something here? I can do this query directly in MySQL but I feel like this should be easy in Drupal to select two columns across two tables.
Thanks!
Comments
You need to join your tables.
You need to join your tables. The Drupal DB doesn't allow for selecting from two tables that way.
I had a feeling that that may
I had a feeling that that may be the reason!
Thanks for this. I'll work towards that. I was thrown off because in raw mysql you dont need to join tables. Is there any reason this is implemented this way?
Strange.. still giving me the
Strange.. still giving me the column errors :
Produces this error :
Any idea why that might happen?
This:
This:
Needs to be this: