Quick Answer: Add Random Dates to MySQL Database

January 30, 2008

Looking for a way to add random dates to a column in one of your MySQL tables?

I ran across this problem and found a straightforward solution. By utilizing str_to_date(), concat(), floor() and rand() you can specify a random date for a column.

update  mytable
set     mycolumn = str_to_date(
                               concat(
                                      floor(1 + rand() * (12-1)), 	/* Generate a random month */
                                      '-',
                                      floor(1 + rand() * (28 -1)), 	/* Generate a random day */
                                      '-',
                                      '2008'
                                      ),
                                '%m-%d-%Y'
                                );

Creating VIEWs in MySQL

January 3, 2008

The MySQL VIEW acts like a virtual table. Each VIEW is actually a SQL statement that has been previously defined.

To create a VIEW:

CREATE VIEW view_name AS
sql_statement;

A basic VIEW could be created as the following:

CREATE VIEW Cities AS
SELECT     DISTINCT(City)
FROM       Customers
ORDER BY   City ASC;

When we run a SHOW COLUMNS FROM Cities MySQL returns the following:

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| city  | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.16 sec)

To access the information from the newly created VIEW:

SELECT     City
FROM       Cities
LIMIT      5;

And MySQL returns:

+----------------+
| city           |
+----------------+
| 100 MILE HOUSE |
| ABBOTSFORD     |
| ABBOTT PARK    |
| ACHESON        |
| ACTON          |
+----------------+
5 rows in set (0.09 sec)

I know this example is incredibly simple, but I hope you can see the potential for using VIEWs. The creation syntax for VIEWs also includes the capability to assign an algorithm type (either AUTO, MERGE or TEMPTABLE) and there are some limitations (not being able to access user variables). The example that I have included is the most simplified method for creating a new view.

With the MySQL VIEWs I am able to simplify other-wise complicated SQL statements. To generate summary level details (by using several aggregate functions) and JOIN that information to more readable SQL, I am saving myself a lot of debugging and troubleshooting if future modifications are required.

For example if I was working on a bulletin board application and I want to display a summary for a user:

CREATE VIEW user_summary AS
SELECT     Users.UserID,
           COUNT(Comments) AS user_comment_count,
           COUNT(Posts) AS user_post_count
FROM       Users LEFT JOIN
           (
            Comments LEFT JOIN Posts
            ON
             (
              Comments.PostID = Posts.ID
             )
            )
            ON
             (
              Users.UserID = Comments.UserID
             )
GROUP BY   Users.UserID;

Now I have a VIEW that contains the bulletin board summary information for the users. To JOIN the VIEW to another SQL statment:

SELECT    Users.UserID,
          user_summary.user_comment_count,
          user_summary.user_post_count
FROM      Users LEFT JOIN user_summary
          ON
           (
            Users.UserID = user_summary.UserID
           );

If you have any questions, or would like further clarification. Please leave me a comment and I will answer you promptly.


Join Tables Across Different Databases

January 2, 2008

Joining tables in queries across different MySQL databases is possible if you use fully qualified database and table names.

Both databases will need to be on the same server for this to work 😉

SELECT  db1.table1.column1,
        db2.table1.column1
FROM    db1.table1 LEFT JOIN db2.table1
        ON
          (
            db1.table1.column2 = db2.table1.column2
          );

The example query will pull two columns (column1 from both databases) and display the results of the LEFT JOIN.


Introduction to Regular Expressions in MySQL

January 2, 2008

Using the LIKE clause can only get you so far when you are building search functionality into your applications. Implementing Regular Expressions will open your search functionality far beyond the most complicated LIKE statement you can create.

SELECT    City
FROM      Customers
WHERE     City REGEXP 'vancouver'
ORDER BY  City ASC;
SELECT    City
FROM      Customers
WHERE     City REGEXP '.ancouver'
ORDER BY  City ASC;

The “.” is used to replace a single character in your search string.

The querys above would work the same if you replaced REGEXP with LIKE (and used wildcards). The main difference between LIKE and REGEXP is that LIKE matches entire column values (unless wildcards are used), and REGEXP automatically searches for matches within the column values.

SELECT     City
FROM       Customers
WHERE      City REGEXP "Vancouver|Richmond"
ORDER BY   City ASC;

Searching for multiple items can be accomplished by separating the searches with the OR operator (|).

SELECT     City
FROM       Customers
WHERE      City REGEXP '[St|Saint] Johns'
ORDER BY   City ASC;

[] is used to specify AND as a operator, [12345] and [1|2|3|4|5] are the same.

It is important to note that ‘St|Saint Johns’ and ‘[St|Saint] Johns’ will not produce identical results. Below are the example queries and their respective output:

SELECT    Distinct(City)
FROM      Customers
WHERE     City REGEXP '[st|saint] john';
+--------------+
| City         |
+--------------+
| ST JOHN'S    |
| SAINT JOHN   |
| FORT ST JOHN |
+--------------+
3 rows in set (0.09 sec)
SELECT     City
FROM       Customers
WHERE      City REGEXP 'st|saint john';
+-----------------------------+
| City                        |
+-----------------------------+
| ESTEVAN                     |
| FORESTBURG                  |
| ST-ISIDORE                  |
| ESTERHAZY                   |
| EAST YORK                   |
...
| ALVINSTON                   |
| EAST PROVIDENCE             |
+-----------------------------+
148 rows in set (0.14 sec)

The ‘st|saint john’ query is requesting all cities that include the letters ‘st’ OR ‘saint john’, whereas the ‘[st|saint] john’ query requested the letters ‘st john’ AND ‘saint john’. Big difference!

[1-9] is shorthand for [123456789]

[a-e] is shorthand for [abcde]

If you are still with me on the REGEXP syntax, my next post will go further into searching for multiple instances and repetition in MySQL.


									

SELECT INTO OUTFILE

December 31, 2007

There are times when, for example I am about to run a large database update or compare two tables, I need to output a simple copy of a SELECT statement to a file for reference at a later date.

After creating a script to handle the task, I realized that MySQL will do all this work for me. All I need to do is create the SELECT query, and append the INTO OUTFILE syntax to generate my file.

Below is a simple example of SELECT … INTO OUTFILE:

SELECT   *
FROM     Customers
INTO OUTFILE 'C:/MySQL_Output/Customers_Output.txt'
FIELDS  TERMINATED BY '|'
        ENCLOSED BY '"'
LINES TERMINATED BY '\n';

We are asking MySQL to SELECT all records from the Customers table and output the query results to the MySQL_Output folder in a file called Customers_Output.txt with some specific formatting. In the example above I am asking MySQL to terminate all fields from the result set with a | and enclose each field with a ” and at the end of a result set, output a new line.

The output from such a request would look like the following:

"ABC Widgets"|"12345 Simple Street"|"Richmond"|"British Columbia"
"XYZ Fasteners"|"78910 Enterprise Avenue"|"Vancouver"|"British Columbia"