How do i create a mysql query in excel?

The following is an example walkthrough of importing an Excel document into a MySQL database. To run this tutorial you will need an Excel file, and admin access to a running MySQL instance.

For the example we’ll be using the following Excel file on rental boats:

boats.xlsx

  1. Open your Excel file and click Save As. Choose to save it as a .CSV (Comma Separated) file. If you are running Excel on a Mac, you will need to save the file as a Windows Comma Separated (.csv) or CSV (Windows) to maintain the correct formatting.

  2. Log into your MySQL shell and create a database. For this example the database will be named boatdb. Note that the --local-infile option is needed by some versions of MySQL for the data loading we’ll do in the following steps.

    $ mysql -u root -p --local-infile
    mysql> create database boatdb;
    mysql> use boatdb;
    

  3. Next we’ll define the schema for our boat table using the CREATE TABLE command. For more details, see the MySQL documentation.

    CREATE TABLE boats (
    d INT NOT NULL PRIMARY KEY,
    name VARCHAR(40),
    type VARCHAR(10),
    owner_id INT NOT NULL,
    date_made DATE,
    rental_price FLOAT
    );
    

  4. Run show tables to verify that your table was created.

    mysql> show tables;
    +------------------+
    | Tables_in_boatdb |
    +------------------+
    | boats            |
    +------------------+
    

  5. Now that there is a database and a table setup, the data can be imported with the LOAD DATA command.

    LOAD DATA LOCAL INFILE "/path/to/boats.csv" INTO TABLE boatdb.boats
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (id, name, type, owner_id, @datevar, rental_price)
    set date_made = STR_TO_DATE(@datevar,'%m/%d/%Y');
    

If you are a Chartio user, you can now connect the MySQL database to Chartio and chart away.

When you use MySQL to query a database, you have the ability to view results from that query on your screen or send them to a text file. If you insert tab characters into the output data stream, you can generate a file that Excel can import. You don’t have to write code to add tab characters to this file; MySQL inserts them automatically when you learn to embed them in your query.

Create Tab-delimited File

  1. Launch the tool you use to manage MySQL and open one of your databases. Use the tool to write a simple query such as this:

    SELECT Column1, Column2 INTO OUTFILE "myFile" FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' FROM myTable

    Replace “Column1” and “Column2” with the names of two fields in one of your database’s tables.

  2. Replace “myFile” with the name of the output file you wish to generate. For example, if you’d like to store a file named “Results.txt” in a folder named “Sales” on your C drive, replace “myFile” with the following text:

    "C:/Sales/Results.txt"

    Note the forward slash characters. You must use a forward slash instead of the regular backward slash as a separator when specifying the directory path to a file.

  3. Replace the word “myTable” in the query with the name of the table from which you wish to retrieve your data. Run your query as you usually do. Results will not appear on-screen this time. Because you used the “INTO OUTFILE” modifier in your query, the results go to the file you specified in the query.

View in Excel

  1. Launch Excel and press “Ctrl-O” to view the Open window.

  2. Navigate to the folder that contains the file you created. Double-click that file to launch Excel’s "Text Import" wizard window.

  3. Click “Next” and then click the “Tab” check box to place a checkmark in that check box if none exists. The data in the file will appear in the window’s "Data Preview" section.

  4. Click “Finish” to load the data into a new spreadsheet.

How do I run a MySQL query in Excel?

Start Excel, click the Data tab. In the appeared ribbon, click From Other Sources, and then click From Microsoft Query. In the next dialog, choose the data source you want to connect to (e.g., using data source name - Devart ODBC MySQL). Uncheck Use the Query Wizard to Create/Edit Queries and click OK.

How do I create a SQL query in Excel?

In Excel, select Data > Queries & Connections, and then select the Queries tab. In the list of queries, locate the query, right click the query, and then select Load To. The Import Data dialog box appears. Decide how you want to import the data, and then select OK.

How do I setup MySQL in Excel?

Overview. Download and install the MySQL for Excel. Connect to your MySQL database. Get help..
In Excel, on the Data tab, click MySQL for Excel to launch the add-in..
In the "MySQL for Excel" panel (near the bottom), click New Connection..
In the "MySQL Instance Connection" screen:.

Can you query a SQL database in Excel?

To connect Excel to a database in SQL Database, open Excel and then create a new workbook or open an existing Excel workbook. In the menu bar at the top of the page, select the Data tab, select Get Data, select From Azure, and then select From Azure SQL Database.