Home > ColdFusion, MySQL, Web Coding > CFSelect with multiple query columns

CFSelect with multiple query columns

December 16th, 2009

CFSelect is nice because it can help you write out some code quicker. Just like the rest of ColdFusion. But I never used it because so many times I needed to display two columns in the <option>, such as “#lastName#, #firstName#”.

While working on a site I fell into the same routine. I start typing out CFSelect then grit my teeth when I get to the Display attribute. Being stubborn about it I stumbled upon a nice MySQL function called CONCAT(). Using that one can merge two columns in the query to be output as a single column name. Like so:

<cfquery name="emps" datasource="#application.datasource#">
SELECT
 id, CONCAT(lastname, ', ', firstname) AS fullName
FROM
 employees
ORDER BY
 lastname, firstname
</cfquery>

Then simply use that column name for the display (or elsewhere needed):

<cfselect name="employeeid" query="emps" selected="" value="id" display="fullName"/>

ColdFusion, MySQL, Web Coding , , , , , , ,

  1. me
    December 16th, 2009 at 09:59 | #1

    Think MSSQL would be something like

    SELECT Surname+', '+Forename AS fullname FROM users
  2. December 16th, 2009 at 10:20 | #2

    @you
    Yes, that’s 100% correct syntax for MSSQL. I just tried it out – thanks for the tip.

  1. No trackbacks yet.