Select Box - Advanced mySQL - labels

2 posts by 2 authors in: Forums > CMS Builder
Last Post: November 17, 2010   (RSS)

Re: [rjbathgate] Select Box - Advanced mySQL - labels

By Dave - November 17, 2010

Hi Rob,

For most users I'd say that's just not possible, but CMSB does support custom MySQL if you know it, and I know you're fairly technical so here's some pointers.

What you want to do (or learn about) is MySQL Joins:
http://www.google.com/search?q=mysql+join+tutorial

I'd practice in MySQL Query Browser or MySQL Console (our plugin) and get your query working first. It will be way faster that reloading the edit page over and over to see if it works.

Here's an example with a CMS setup with a 'news' section, 'accounts' section and a table prefix of "cms_":

SELECT cms_news.num, cms_accounts.fullname
FROM cms_news
LEFT JOIN cms_accounts ON cms_news.createdByUserNum = cms_accounts.num


Since you're dealing with multiple tables you need to put them before the fieldnames so MySQL knows which table the field is coming from. You can use table aliases to make the code shorter (and hopefully easier to read), just put an alias word after the tablename like this:

SELECT n.num, a.fullname
FROM cms_news n
LEFT JOIN cms_accounts a ON n.createdByUserNum = a.num


And here's a version using concat:
SELECT n.num, CONCAT_WS(' - ', a.fullname, a.username)
FROM cms_news n
LEFT JOIN cms_accounts a ON n.createdByUserNum = a.num


Hope that helps! Let me know any questions.
Dave Edis - Senior Developer
interactivetools.com