1

There are some views in my site. I have configure the setting to fetch the Views query. I want to convert those query to MySQL query to run & getting the result directly.

How can I do this?

2 Answers 2

6

Try the Views Show Query module

This module shows real SQL query in Views UI during live preview.

'Real query' means it is rewritten by query_alter hooks. For instance, node access checks are added, so this requires selecting the user on behalf of which the query is supposed to be executed. So the module adds 'User ID to show final query' field in views preview form. You must enter numeric user ID in this field to view the real SQL query.

This module makes it possible to show SQL query not only during live preview, but when the view is executed normally. You can add footer to the view with the following PHP code:

<?php
$rows = array();
$view->exposed_input['views_show_query_uid'] = 1;
views_show_query_views_preview_info_alter($rows, $view);
drupal_set_message($rows['query'][0]);
?>

OR

There another module called Query Coder which Convert SQL query to Drupal Database abstraction layer code.

enter image description here

1
  • also thanks @KrishnaMohan, I didn't know about Query Coder! excellent module. I've got some complex data time queries to test it with that I could not get working with the DBA, I hope it can. Nov 9, 2014 at 11:02
0

Also worth mentioning, just in case you didn't know, Views will show you the SQL that it generates and uses if you turn on its visibility in the views settings page

  • go to /admin/structure/views/settings
  • Check - Show information and statistics about the view during live preview
  • Check - Show the SQL query

However, this is query can be seriously complex if you have lots of relationships and contextual filters. The other modules recommended by @KrishnaMohan make it a lot more digestible. But it is something that it does natively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.