Archive for the ‘Joomla’ Category

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);