How to write SQL Statements With Spaces In Column Names 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()-&gt;select('Product_Details', 'n');
$query-&gt;addField('n', 'S.No.');
$query-&gt;condition('n.Product type', 'Irons');
$results = $query-&gt;execute()-&gt;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] =&gt; 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?
You can use ` try with:

$query = \Drupal::database()-&gt;select('Product_Details', 'n');
$query-&gt;addField('n', 'S.No.');
$query-&gt;condition('n.`Product type`', 'Irons');
$results = $query-&gt;execute()-&gt;fetchAll();


According to this post https://stackoverflow.com/questions/14190798/how-to-select-a-column-name-with-a-space-in-mysql you can use also [].