In PHP we would use UCFIRST, so how do we do this in MySQL? SELECT CONCAT(UPPER(SUBSTRING(firstName, 1, 1)), LOWER(SUBSTRING(firstName FROM 2))) AS properFirstName You could also create your own MySQL function of this and then just call it, instead of
How to display database records which are so many days old
Have a date field in your table which includes the date the record was inserted, try a TIMESTAMP data type, for our example our field is called ‘DATE_ADDED’ and then use the DATEDIFF MySQL function. Then run the following type
How to select a random set of records using MySQL
ORDER BY RAND() combined with LIMIT is useful for selecting a random sample of a set of rows: SELECT * FROM <tablename> ORDER BY RAND() LIMIT 1000; Note that RAND() in a WHERE clause is re-evaluated every time the WHERE
How to load up a file into the database, which was created using mysqldump which includes SQL statements
mysql -h<host name> -u<username> -p<password> <database name> < my-dump.file
How to dump the contents of 1 table from MySQL into a file, that includes SQL type INSERT statements
mysqldump -h<host name> -u<username> -p<password> <db name> <table name> > my-dump.file TIP: When dumping tables or databases, it’s worth adding –quote-names as a parameter, just in case you may have used a reserved word as a table or column name.
How to dump the entire contents of a database from MySQL into a file, that includes SQL type INSERT statements
mysqldump -h <host name> -u<username> -p<password> <db name> > my-dump.file
How do I extract the 1st position of a character string that are numbers and then order by number using MySQL
This can be accomplished using one select statement. It involves using the REPLACE, LEFT and CAST functions. Say we have this string: 1. This is no. 1 9. This is no. 9 10. This is no. 10 If you use
How do I get the last occurence of a character from a string using MySQL
This is a tricky one, but the best way to go about it is to first reverse the string then find the first occurence of the string. Then subtract the length of the string from the location of the string
How do I get the latest date and time in an SQL query
The best way of doing this is to use the command now() in your sql statement for both date and time, e.g.: INSERT INTO MYTABLE (MYDATE, DESCRIPTION) VALUES (now(), description); If you just want the date only then use CURDATE()
How do I get a list of my MySQL tables?
Write a SELECT that includes the following text: SHOW TABLES FROM <databaseName>; This will present you with a list of the MySQL data dictionary type tables in your database, you can then also add a ‘WHERE’ or ‘LIKE’ command to
