Saturday, July 4th, 2009

Archive for 'MySQL'

How to select rows randomly

Did you ever need to select rows randomly from your database?

Read more

Get maximum ID from a table

To get the maximum id from a table you can use the MAX() function.
Here’s an example:
SELECT MAX(id_field_here) as max_id FROM `table`;

Read more

How to make a selection based on the ending value

An SQL command that selects the field

Read more

How to make a selection based on the beginning value

An SQL command that selects the field

Read more

How to make an alphabetical search

Hello,
This tutorial is aimed to help you how to make an alphabetical search using MySQL. Many sites use it these day and we will show you how do they do it.
Let’s suppose you have a business directory and you want to display the listings that are starting with a specific letter or a non-letter character [...]

Read more

How to add a prefix to a field’s value

Greetings,
Ever wanted to add a preffix to a mysql field’s value? The CONCAT string function can help you. Here’s how you can do it:

UPDATE `your_table` SET field_name = CONCAT(’my prefix here ‘, field_name)
where field_name=’the field value here’; #optional condition

Example: You can use this command to add a prefix for a telephone number.
Good luck!

Read more

How to add a suffix to a field’s value

Greetings,
Ever wanted to add a suffix to a mysql field’s value? The CONCAT string function can help you. Here’s how you can do it:

UPDATE `your_table` SET field_name = CONCAT(field_name, ‘ my suffix here’)
where field_name=’the field value here’; #optional condition

So if the field’s value if “New York” and you add the suffix ” City” then our [...]

Read more

Update table field by replacing a string value with a new one

Greetings,
Let’s suppose you have a mysql database and you need to update only a part of a field’s value, not all.
This can be done using the following query:
UPDATE `table` SET `field` = REPLACE(`field`, “old_string”, “new_string”);

Here’s how it will look like in a PHP Code:

<?php
$db_host = ‘db_host’; // usually localhost
$db_user = ‘db_username’;
$db_pass = ‘db_password’;
$db_name = ‘db_name’;

$db [...]

Read more