Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly generate SELECT statement for a table in DataGrip?

In Microsoft SQL Server Management Studio (SSMS), you can right-click on a table, then go to Script Table as -> SELECT To, and then choose a destination for the generated script.

Is there anything similar in DataGrip, or can one be custom created in some way?

The reason I find this useful is because I often find that I'm working with a table with a lot of columns, and I want to select all but a few of them. So it's easier to just have it generate the SELECT statement with all the columns explicitly listed out, so that I can just go through and delete the ones I don't want.

Currently, my workaround in DataGrip is to right-click the table, then choose Copy DDL. This generates the CREATE TABLE statement, which lists out all the columns, but it also includes the column definitions. So I have to do a regex replace or run a macro to get rid of the extra info, which is kind of a pain. Does anyone have a better solution?

like image 555
Travesty3 Avatar asked Jan 06 '17 17:01

Travesty3


People also ask

How do I query a table in DataGrip?

In other IntelliJ-based IDEs, Ctrl+Alt+F8 on an object gives you a quick evaluation. In DataGrip, invoke it on a table in a query to see the data of that table. Ctrl+Alt+F8 on a column name will show the values of that column in the expected result-set.

How do you create a new table from SELECT statement?

You can create one table from another by adding a SELECT statement at the end of the CREATE TABLE statement: CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl; MySQL creates new columns for all elements in the SELECT .

Can SELECT statement retrieve data from table?

An SQL SELECT statement retrieves records from a database table according to clauses (for example, FROM and WHERE ) that specify criteria. The syntax is: SELECT column1, column2 FROM table1, table2 WHERE column2='value';


2 Answers

In DataGrip as other intellij-based IDEs everything is about source editing. So that can be achieved that way:

  • Open console
  • Start typing sel, you'll get completion popup (if not, hit Ctrl+Space)
  • Select sel here, which is live-template for select statement
  • Select statement will be generated, asking for table name and column list enter image description here
  • Select desired table from completion, as column list provide *
  • Then hit Alt+Enter on asterisk and select Expand column list enter image description here

I suggest you to look through https://www.jetbrains.com/datagrip/features/

like image 68
kassak Avatar answered Oct 24 '22 04:10

kassak


In DataGrip 2018.3 you can use postfix completion. This is the flexible way to get needed queries.

Try typing

SELECT %table_name%.from
SELECT %table_name%.afrom
SELECT %table_name%.join

And this will be expanded to the needed queries. In the case of from completion you'll be able to write columns.

This makes writing SQL more logical: first, you point table, then columns.

See gif: enter image description here

like image 25
moscas Avatar answered Oct 24 '22 04:10

moscas