MySQL output query to file

Ways to output the results of a MySQL query to a file:

  1. Use the command line switch option -e to input the query and > to redirect the output to a file:
    % mysql -uuser -ppass -e "SELECT id, name FROM person WHERE name like '%smith%'" database > smiths.txt
    
  2. Use the SQL query construct INTO OUTFILE 'filename' to write the results to file from inside the MySQL command line interface:
    % mysql -uuser -ppass database
    mysql> SELECT id, name INTO OUTFILE '/tmp/smiths.txt' FROM person WHERE name like '%smith%';
    Query OK, 10 rows affected (0.01 sec)
    

    Be sure where you have write permissions where the output file will be written to. Specify the full path if in doubt.

 MySQL clear query cache

MySQL clears the query cache after any changes to the tables used in a cache query. Any of INSERT, UPDATE, DELETE, TRUNCATE, ALTER, DROP TABLE or DROP DATABASE potentially remove queries from the cache.

You can manually clear the query cache with RESET QUERY CACHE.

 Return random records in MySQL

Works only for relatively small tables.

SELECT field1, field2, field3 FROM mytable WHERE status = 1 AND otherfield = 'somevalue' ORDER BY RAND();

One of many sources:
http://davidwalsh.name/return-random-records-mysql

 Converting to MySQL TIMESTAMPs from int(11)

Update: Imported on 6 Oct 08 from http://kitt.hodsden.com/

For MySQL 5+

mysql> SELECT FROM_UNIXTIME(created) FROM node WHERE nid = 10;
+------------------------+
| from_unixtime(created) |
+------------------------+
| 2005-05-22 10:22:04    | 
+------------------------+

For MySQL 4.1 and lower:

Having converted MySQL TIMESTAMPs to Drupal's int(11) style timestamps, which records the number of seconds since epoch (midnight, January 1st 1970), I also had cause to convert the other direction: from an int(11) to a MySQL TIMESTAMP.

MySQL provides a utility function to convert from unix timestamps (the int(11) format): FROM_UNIXTIME.

From the MySQL documentation:

FROM_UNIXTIME(unix_timestamp) , FROM_UNIXTIME(unix_timestamp,format)

Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric context.

mysql> SELECT FROM_UNIXTIME(875996580);
        -> '1997-10-04 22:23:00'
mysql> SELECT FROM_UNIXTIME(875996580) + 0;
        -> 19971004222300

If format is given, the result is formatted according to the format string. format may contain the same specifiers as those listed in the entry for the DATE_FORMAT() function.

mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),
    ->                      '%Y %D %M %h:%i:%s %x');
        -> '2003 6th August 06:22:58 2003'

Okay, so FROM_UNIXTIME can format the time/date in pretty much any way you'd like it, with the optional format argument.

Since we want the TIMESTAMP format, force MySQL to treat the date as an integer by adding 0 to it:

mysql> SELECT FROM_UNIXTIME(creation_date) + 0 FROM paid_support LIMIT 1;
+----------------------------------+
| FROM_UNIXTIME(creation_date) + 0 |
+----------------------------------+
|                   20031102084514 |
+----------------------------------+
1 row in set (0.00 sec)
Syndicate content