Hi I am trying to write a Dynamic Query to get data from the database but I am facing issues to write a query for column names with white spaces.
My Product_Details Table looks as below:
| S.No. | Product line | Product type | Product | Year |
| —— |:————–:| ————-:|———-:|——:|
| 1 | Camp Equipment | Cooking Gear | Cook Set | 2015 |
| 2 | Golf Equipment | Irons | Titanium | 2014 |
My SQL Looks as below:
$query = Drupal::database()->select('Product_Details', 'n'); $query->addField('n', 'S.No.'); $query->condition('n.Product type', 'Irons'); $results = $query->execute()->fetchAll();
But I am getting Syntax error or access violation.
While looking into the error log after SQL Query executed I found out this:
1: SELECT n.S.No. AS SNo FROM {Product_Details} n WHERE n.Producttype = :db_condition_placeholder_0; Array ( [:db_condition_placeholder_0] => Irons )
If you look into it Product type is changed to Producttype and also S.No. is changed to SNo
Anyone got the idea how to write the SQL Query when column names has spaces?