Selecting Individual Values In A Database With MySql (DISTINCT) In Joomla

Posted: December 19, 2012 in Joomla

Wanted to show you how easy it is to use the DISTINCT function in MySQL inside of Joomla.

This is very useful whenever you want to create a list of all the individual values of a specific field in a database table. It only gets that value once, so like below, there is several “Burlington” values in the city field

Table: jos_locations

id name city state
1 Coffee House Graham NC
2 Barnes & Nobile Burlington NC
3 Sheetz Mebane NC
4 Starbucks Burlington NC
5 Panera Burlington NC

So this is how you would use distinct to just grab that list of specific values.

// Get a database object
$db = &JFactory::getDBO();
$query =  ' SELECT DISTINCT city '.
          ' FROM #__ locations ';

Now we want to Set the Query.

$db->setQuery($query);

Finally we send the query and return the Results and print it out.

$results = $db->loadObjectList();
print_r($results);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s