To round a number in MS SQL use the ROUND() function. This function takes two parameters, the first is the number to be rounded and the second is the number of decimal places to round the number to. Here is an example of rounding the numbers in a column to the nearest whole integer.
If you have a table of incremental values it can be hard to find out which ones are missing. The only solution might be to write a script to get all the data from the database and see which ones are missing. However, there is a way of doing this without using a script.
Using a standard select query like this:
SELECT * FROM table;
Gets the following data:
1 3 10 23
We can see that values are missing, but which ones? The following query will show us where the gaps are in the data of the table.
SELECT t1.id+1 as Missing FROM table as t1 LEFT JOIN table as t2 ON t1.id+1 = t2.id WHERE t2.id IS NULL ORDER BY t1.id;
Produces the following result.
If you find that you are having trouble sorting data in a VARCHAR column in a MySQL database then you can try the following trick.
Lets say that you had the values 1,200,30,4000 and 5 and that you inserted them into the database in that order. When the following query is run on this data:
SELECT numbers FROM table ORDER BY numbers;
The following output is seen.
1 200 30 4000 5
This is clearly not the correct order, although it represents the order of. You can force a natural order to the sort by using a "+0" after the colum you are trying to sort by.
SELECT numbers FROM table ORDER BY numbers+0;
This produces the following output, which is sorted as you expect a set of numbers to be sorted.
Rather than work out how many years have passed since and event, or how old something is, after you get the data from a MySQL database you could use the following query to convert the date on the MySQL side. It works by subtracting the current date from the given date and formatting it in years. Adding the given value to 0 casts the string given by DATE_FORMAT() into an interger.
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS('2000-07-23 09:20:59')), '%Y') + 0;
This gives the result of 8. You can also pass in just the year, month and day string to the function.
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS('2000-07-23')), '%Y') + 0;
This gives the result of 38.
The function requires that a properly formatted MySQL date string is found or it will produce an error. The following string used in this function.
phpMyAdmin is a tool, written in PHP, that allows you to handle the administration of a MySQL database server. You could always download the MySQL GUI tools, but the problem there is that you need to give external access to a user account, which isn't always possible to do. This is where phpMyAdmin steps in.
The tool is easy to use and I have done some tricky stuff with it in the past. It can do everything that you need it to do with MySQL.
In MySQL the OPTIMIZE TABLE can be used if you have made changes or have deleted large parts of the table.
Any deleted rows are kept behind the scenes in the server in order to allow the reuse of these spaces. The OPTIMIZE TABLE command reclaims the unused space and defragments the data file.
For a normal MyISAM table the OPTIMIZE command works in the following way.
- If the table has deleted or split rows, repair the table.
- If the index pages are not sorted, sort them.
- If the table's statistics are not up to date (and the repair could not be accomplished by sorting the index), update them.
You can do this automatically by using the following PHP code.
By using the MySQL command.
SHOW TABLE STATUS FROM database;
You can get all sorts of information about a database. The query returns each table as a row and gives lots of information about each table. Using this query it is possible to work out some usage data for the database as a whole. The following function will take a database name and a database resource handle and return how big that database is, the number of tables, and the number of rows in those tables. You will probably have a maximum limit to the amount of data that you can store in your database, so this function is useful to make sure that you don't exceed this limit. This function also uses another function found on the #! code site called readableFileSize() to give more meaningful data sizes.
The following query will create a random vote of between 50 and 100 for each rateable value.