leftJoin help needed

8 posts by 3 authors in: Forums > CMS Builder
Last Post: November 11, 2011   (RSS)

By Deborah - November 9, 2011

I 'm having trouble joining two tables. 'home_pg_slideshow' has a select list titled 'short_title' based on the field 'short_title' in the table 'furniture_categories'. I want to display the field 'short_title', and other fields from the 'furniture_categories' table. My code:

list($home_pg_slideshowRecords, $home_pg_slideshowMetaData) = getRecords(array(
'tableName' => 'home_pg_slideshow',
'where' => 'home_pg_slideshow.num = ' . getNumberFromEndOfUrl(),
'debugSql' => true,
'leftJoin' => array(
'furniture_categories' => 'short_titleNum',
),
));
$home_pg_slideshowRecord = @$home_pg_slideshowRecords[0]; // get first record


Note the MySQL error shown in the debug:

SELECT SQL_CALC_FOUND_ROWS `home_pg_slideshow`.*,
furniture_categories.`num` as `furniture_categories.num`,
furniture_categories.`title` as `furniture_categories.title`,
furniture_categories.`short_title` as `furniture_categories.short_title`,
furniture_categories.`abbreviation` as `furniture_categories.abbreviation`,
furniture_categories.`master_category_title` as `furniture_categories.master_category_title`,
furniture_categories.`master_landing_pg_url` as `furniture_categories.master_landing_pg_url`,
furniture_categories.`landing_pg_url` as `furniture_categories.landing_pg_url`,
furniture_categories.`category_page_heading` as `furniture_categories.category_page_heading`,
furniture_categories.`category_pg_description` as `furniture_categories.category_pg_description`
FROM `cms_home_pg_slideshow` as `home_pg_slideshow`
LEFT JOIN `cms_furniture_categories` AS `furniture_categories` ON home_pg_slideshow.`short_titleNum` = furniture_categories.num
WHERE (home_pg_slideshow.num = 0)
ORDER BY dragSortOrder DESC

MySQL Error: Unknown column 'home_pg_slideshow.short_titleNum' in 'on clause'

-------------------

In the table 'home_pg_slideshow' pulldown list for 'short_title' I'm using for get options from database (advanced):
tablename of furniture_categories
option values - num
option labels - short_title

------------------

I was working with this forum post:
http://www.interactivetools.com/iforum/Products_C2/CMS_Builder_F35/JOIN_question..._I_think_P71124/

I'm hoping someone can help me out. I seem to be very close to making this work.

~ Deborah

Re: [Deborah] leftJoin help needed

By Jason - November 9, 2011

Hi Deborah,

Try changing short_titleNum to short_title like this:

list($home_pg_slideshowRecords, $home_pg_slideshowMetaData) = getRecords(array(
'tableName' => 'home_pg_slideshow',
'where' => 'home_pg_slideshow.num = ' . getNumberFromEndOfUrl(),
'debugSql' => true,
'leftJoin' => array(
'furniture_categories' => 'short_title',
),
));
$home_pg_slideshowRecord = @$home_pg_slideshowRecords[0]; // get first record


That should take care of that error.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] leftJoin help needed

By Deborah - November 9, 2011

Hi, Jason. Thank you. Yes, that change took away the error.

Now in attempting to display the listings, I think because I'm not searching (just displaying a full list) I need to adjust something relating to the following line;

'where' => 'home_pg_slideshow.num = ' . getNumberFromEndOfUrl(),

With the above line in place I get 'no records found'. (which makes sense)
Without that line I see all of my 'home_pg_slideshow' records, but for the fields joining from the other table the first selection is repeated for each record, instead of their unique values.

Any idea what I need to change? Thanks again.
~ Deborah

Re: [Deborah] leftJoin help needed

By Jason - November 10, 2011

Hi Deborah,

Another approach would be to not use a join, but do a separate query to the furniture category section like this (note, this assumes you're using version 2.08 or higher):

list($home_pg_slideshowRecords, $home_pg_slideshowMetaData) = getRecords(array(
'tableName' => 'home_pg_slideshow',
'where' => 'home_pg_slideshow.num = ' . getNumberFromEndOfUrl(),
));
$home_pg_slideshowRecord = @$home_pg_slideshowRecords[0]; // get first record

$furnitureCategory = mysql_get('furniture_categories', null, "num = ".$home_pg_slideshowRecord['short_title']);


The mysql_get() code could also be put inside a foreach loop if you were displaying multiple homepage_pg_records

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] leftJoin help needed

Jason, this site is using 2.12.

I tried the new code, but see a MySQL error in debug:

SELECT SQL_CALC_FOUND_ROWS `home_pg_slideshow`.*
FROM `cms_home_pg_slideshow` as `home_pg_slideshow`
WHERE (home_pg_slideshow.num = 0)
ORDER BY dragSortOrder DESC

MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1

------------------

And yes, I do wish to display multiple homepage_pg_slideshow records.

Thanks for your help on this.
~ Deborah

Re: [gkornbluth] leftJoin help needed

Jerry,

Thank you for your well-detailed post. I started with the instructions you posted here and then logged in to your CMSB Cookbook for further assistance...

... and I got my page working! [insert happy face here]

I appreciate your assistance very much!

~ Deborah

Re: [Deborah] leftJoin help needed

You're very welcome.

Glad it worked

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php