Category: MySQL

Random Number Range In MySQL

5 April, 2008 | MySQL | No comments

To create a number between one value and the next you can use the following formula, where i is the lower end of the range and j is the higher end of the range.

FLOOR(i + RAND() * (j – i))

Rather than put in (j-i) in your query you should put in the result. So for a number between 1 and 10 you would make i = 1 and j = 11. 11-1 = 10 so the query would run like this.

SELECT FLOOR(1 + (RAND() * 10));

For a number between 64 and 104 you would use the following query.

SELECT FLOOR(64 + (RAND() * 41));

Finding The Lastest Timestamp Value From A Set Of Values In MySQL

20 March, 2008 | MySQL | No comments

Storing sets of data with a timestamp is common practice, especially if you want to keep a history of the values that have been stored previously. Lets say you wanted to store a piece of information about two variables, each of which can have a history. Here is an example dataset.

Value1 Value2 Data Timestamp
1 1 1 1206057387
1 2 1 1206057387
1 3 1 1206057387
1 1 1 1206059020
1 3 1 1206059020
2 1 1 1206057387
2 2 2 1206057387
2 3 3 1206057387
2 1 1 1206059020

Value2 has been updated in this table three times. To get the latest data where Value1 is equal to 1 you can use the following query.

SELECT * FROM datatable WHERE datatable.timestamp >= (SELECT MAX(timestamp) AS time FROM datatable WHERE Value1 = 1) ORDER BY Value2 ASC;

This uses a subquery as part of the WHERE clause in order to find the maximum timestamp available for Value2.

If the data items are added over a period of a few seconds you can use the TRUNCATE() function with a negative integer as the second argument to group the timestamp values in a more meaningful way. Using a negative value with TRUNCATE replaces numbers to the right of the number with zeros. Use the following SQL statement to see what the function does with the timestamp.

SELECT UNIX_TIMESTAMP(), TRUNCATE(UNIX_TIMESTAMP(),-8);

This will change the value 1206112846 to 1200000000.

Using the truncate function with the timestamp value in this way can lead to problems if your data is entered every few seconds. If it is the case then you would need to enter values into database using the truncate function so that if two values are entered at more or less the same time the new one replaces the old.

Finding Duplicate Values In A MySQL Table

6 February, 2008 | MySQL | No comments

To find duplicate values you need to use the MySQL COUNT() function and then pick out all of the counts that are greater than one.

SELECT value,COUNT(value) AS Count
FROM test
GROUP BY value
HAVING (COUNT(value) > 1)
ORDER BY Count DESC;

Conversely you can also select the rows that only have a single entry.

SELECT value,COUNT(value) AS Count
FROM test
GROUP BY value
HAVING (COUNT(value) =; 1)
ORDER BY Count DESC;

However, it is very nice to pick out the duplicate entries in a table, but you might still need to do something with them. Here is a query to delete any duplicate rows from a table. It does a simple self join and deletes the row value with the lowest ID.

DELETE bad_rows.*
FROM tests AS good_rows
INNER JOIN tests AS bad_rows ON bad_rows.number = good_rows.number
AND bad_rows.id > good_rows.id;

More information on this deletion query and other methods of deleting duplicates can be found at Xaprb.com.

Logging Onto A MySQL Database

28 January, 2008 | MySQL | No comments

If you have command line access to your MySQL database server you will need to use certain parameters to log in. Most web hosts will not allow you to do this, so you might want to install MySQL into a local computer and give it a go.

To log into mysql you must run the program called mysql with certain parameters. Here is an example.

./mysql -u username

One thing you must realise is that all usernames are associated with a host so if the user you specified can’t access the server from this host then you won’t get far. To specify the host location enter the -h flag.

./mysql -h hostaddress -u username

If your user is able to access this server then you will be asked for a password. You can set the password in the string using the -p flag. If this doesn’t work then leave out the space between the -p and the password.

./mysql -h hostaddress -u username -ppassword

For security reasons the MySQL server might have been told to use a different port than the standard 3306. You can therefore set the port using the -P flag.

./mysql -h hostaddress -u username -ppassword -P 3309

Users are also restricted to the tables and commands that they can use, so you can set a default database to use that database once you have logged in. This is useful to do if you don’t have access to administrative commands like SHOW DATABASES. To set the database to be used just add it to the end of the line, no flag is needed.

./mysql -h hostaddress -u username -ppassword -P 3309 test

You should try to log into the MySQL command line if you are trying to learn SQL, but it is useful if you have some special things to do like data importing or carrying out a backup. One alternative to doing it this way is to get hold of the wonderful MySQL GUI tools which will allow you to anything you can do through the command line without having to have an in depth knowledge of SQL syntax.

Toggle a TINYINT Field in MySQL

31 December, 2007 | MySQL | No comments

MySQL uses the datatype TINYINT to store boolean values. MySQL stores the value as TINYINT(1) which is the same as a bit so the value is either 0 (false) or 1 (true). Using boolean fields can be very useful, but it can be costly in processing as to change the value you have to query the database, find out the value of the field and then act accordingly.

Here is a simple MySQL query that can be used to toggle the value already present in the TINYINT field without having to do any pre-querying.

UPDATE table SET field = 1 - field