Queries 2: Q B (A F V) 6
Queries 2: Q B (A F V) 6
QUERIES 2
Queries
Queries
A query is used to extract data from the database in a readable format according to the
user's request. Navicat for MySQL provides two powerful tools for working with the SQL
queries: Query Editor for editing the query text directly and Query Builder for building
queries visually.
Just simply click to open an object pane for Query. A right-click displays the popup menu
or using the object pane toolbar below, allowing you to create new, edit, open and delete the
selected query.
Create Query
Queries
Hint: To create new query you can also right-click the Queries node of the navigation pane
and select the New Query from the popup menu.
To create a new query with the same properties as one of the existing queries has (using
drag and drop method)
o Copy here
o Move here
o Cancel
Queries
Edit Query
Open Query
Note: Only SELECT queries will be run automatically with results being displayed on Result
tab.
Run Query
To run a query
Queries
To run a saved query from the command line (set schedule)
Delete Query
To delete a query
Queries
Query Builder (Available only in Full Version)
Navicat for MySQL provides a useful tool called Visual Builder for building queries visually.
It allows you to create and edit queries without knowledge of SQL. The database objects are
displayed in left panel. Whereas in the right panel, it is divided into two portions: the upper
Graphical View, and the lower Syntax View.
To open a query in the visual builder (see Edit Query for details).
Note: Visual Builder supports SELECT statement only. Use Query Editor for creating complex
queries.
Queries
Working with Diagram Area
To add a table to the query, simply drag it or double-click from the left panel to the Graphical
View area. To include a table field in the query, check the left of the field name in the list. To
include all the fields, click at the left of the table caption.
To remove the object from the Graphical View area, click the cross button at the object
caption.
Hint: You are also allowed to set criteria by right-clicking any fields from the Graphical View
area.
Queries
Setting Field Association
To associate database objects by two fields, just drag one field from the object list to another
and a line will appear between the linked fields.
Hint: To delete all the links of some object, click button '-' next to the object alias.
Queries
Setting Output Fields
The fields you have selected in the graphical view will be displayed in the Syntax View which
allows you to set their displaying order and modify the output fields of the query using
<distinct>, <func> and <Alias>.
<distinct>
Enable this option if you wish the repeated records are not included into the query
result.
<func>
Set the aggregate functions (SUM, MAN, MIX, AVG, COUNT) for each field.
<Alias>
Change the output query field name.
Queries
Setting Criteria
To add a condition, click the <--> = <--> from the Where Clause in the Syntax View. Click
<--> to choose the field from the list of all the table fields, available in the query. To define
your own criteria, type your values directly in the Edit Tab. Clicking = to set condition
operator.
10
Queries
Setting Grouping Criteria
You can set the conditions for grouping query records from the Group By Clause in the
Syntax View. They are set in the same way as setting criteria. The conditions will be included
into the HAVING statement of the current query.
11
Queries
Setting Sorting Criteria
When you query your MySQL database, you can sort the results by any field in an ascending
or descending order by just adding ORDER BY at the end of your query.
In Visual Builder, you can set the way of sorting query records from the Order By Clause in
the Syntax View. To change the sorting direction, click on either ASC or DESC.
12
Queries
Setting Limit Criteria
Limit Clause is used to limit your MySQL query results to those that fall within a specified
range. You can use it to show the first X number of results, or to show a range from X - Y
results. It is phrased as Limit X, Y and included at the end of your query. X is the starting
point (remember the first record is 0) and Y is the duration (how many records to display).
13
Queries
Query Editor
Navicat for MySQL provides a useful tool called Query Editor for creating and executing
queries. It allows you to create and edit SQL text for a query, prepare and execute selected
queries.
To open a query in the query editor (see Edit Query for details).
Hint: Query text will be automatically generated while you build in Query Builder.
14
Queries
You are allowed to run selected portion of query, just simply right-click the highlighted query
and select Run Selected.
You can define multiple SQL statements in one Editor window, and the editor let you run the
current statement your cursor is on (place your cursor in the front of the desired statement).
Just simply select Run a statement from here or press F7.
Note: Select Run a statement from here or press F7, the next statement will be continue
to run.
15
Queries
Working with Code-Completion (Available only in Full Version)
Code-completion in Navicat displays information in drop-down lists as you type your SQL
statement in the editor, it assists you with statement completion and the available properties
of database objects, for example databases, tables, fields, views etc with their appropriate
icons.
To activate the code-completion, just simply press '.' for the available properties of database
object currently in the scope.
Hint: You may invoke code-completion by pressing Ctrl+SPACE on your keyboard for SQL
keywords.
Note: Code-completion can be also applied on View, Stored Procedures/Functions and Event.
16
Queries
Query Results
To run the query click Run on the toolbar. If the query statement is correct, the query
executes and, if the query statement is supposed to return data, the Result tab opens with
the data returned by the query. If an error occurs while executing the query, execution stops,
the appropriate error message is displayed.
The Result tab displays the result data, returned by the query, as a grid. Data can be
displayed in three mode: Grid View, Form View and Text/Blob View. See Data View for
detail.
The toolbars of Query Result Viewer provides the following functions for managing data:
• Edit TEXT/BLOB
Allow you to view and edit the content of TEXT and BLOB fields.
17
Queries
Query Profile and Status
To show the profile and status when running the query, simply choose View -> Show Profile
and Status and click Run on the toolbar.
The Profile tab displays the query profile: Table lock, System lock, Statistic, etc.
18
Queries
The Status tab displays the query status: Bytes received, Bytes sent, etc.
19
Queries
Query Explain
Explain tab returns a row of information for each table used in the SELECT statement. The
tables are listed in the output in the order that MySQL would read them while processing the
query.
20
Queries
Query Parameters
Query Builder and Query Editor both support using of parameters inside the query text. You
can set query parameters to add variable values to a query each time you run it. The
parameter should appear as an identifier with $ at its beginning, quote with [ ], e.g.
[$any_name].
Execute the query and the Input parameter Dialog is provided for you to enter the desired
data you wish to search.
21
Queries