DataObject Query
| From: | Ian Warner | Date: | Thu, 28 Sep 2006 13:43:34 +0000 |
| Subject: | DataObject Query | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-25279@lists.php.net to get a copy of this message | ||
Hi
I have the following query generated through DO
--
SELECT sms_services.
service_id, sms_services.service, sms_services.watershed, sms_services.free_msgs, sms_services.type, sms_services.multiple_signups, sms_keywords.keyword_id, sms_keywords.keyword, sms_shortcodes.shortcode_id, sms_shortcodes.shortcode,
COUNT(DISTINCT sms_customers_services.customer_services_id) AS total, sms_actions.action_id
FROM sms_services
LEFT JOIN admin.sms_actions ON admin.sms_actions.service_id=sms_services.service_id
LEFT JOIN admin.sms_keywords ON admin.sms_keywords.service_id=sms_services.service_id
LEFT JOIN admin.sms_customers_services ON admin.sms_customers_services.keyword_id=sms_keywords.keyword_id
LEFT JOIN admin.sms_shortcodes ON admin.sms_shortcodes.shortcode_id=sms_keywords.shortcode_id
WHERE ( sms_services.user_id = '2' ) AND ( sms_services.status = 'Active' ) AND ( sms_keywords.status = 'Active' ) AND ( sms_shortcodes.status = 'Active' ) AND ( sms_customers_services.status = 'Active' ) AND ( sms_actions.status = 'Active' )
GROUP BY sms_services.service_id ASC, sms_keywords.keyword_id ASC
--
Now I want it to be -- Notice that the AND statements for the status flag are now within the JOIN syntax - this provides the ability to perform a condition only if data is returned from this table the above performs the status condition on all the data and so retruns NULL as maybe there are no customers or actions with a service. Cant check PEAR at the moment as the site is down.
--
SELECT sms_services.service_id, sms_services.service, sms_services.watershed, sms_services.free_msgs, sms_services.type, sms_services.multiple_signups, sms_keywords.keyword_id, sms_keywords.keyword, sms_shortcodes.shortcode_id, sms_shortcodes.shortcode,
COUNT(DISTINCT sms_customers_services.customer_services_id) AS total, sms_actions.action_id
FROM sms_services
LEFT JOIN admin.sms_actions
ON admin.sms_actions.service_id=sms_services.service_id
AND ( sms_actions.status = 'Active' )
LEFT JOIN admin.sms_keywords
ON admin.sms_keywords.service_id=sms_services.service_id
AND ( sms_keywords.status = 'Active' )
LEFT JOIN admin.sms_customers_services
ON admin.sms_customers_services.keyword_id=sms_keywords.keyword_id
AND ( sms_customers_services.status = 'Active' )
LEFT JOIN admin.sms_shortcodes
ON admin.sms_shortcodes.shortcode_id=sms_keywords.shortcode_id
AND ( sms_shortcodes.status = 'Active' )
WHERE ( sms_services.user_id = '2' ) AND ( sms_services.status = 'Active' )
GROUP BY sms_services.service_id ASC, sms_keywords.keyword_id ASC
==
// Query to get the list of services and assigned keywords for this client
DB_DataObject::debugLevel(1);
$sql1 = DB_DataObject::factory(TBL_SMS_CENTER_SERVICES);
$sql2 = DB_DataObject::factory(TBL_SMS_CENTER_KEYWORDS);
$sql3 = DB_DataObject::factory(TBL_SMS_CENTER_SHORTCODES);
$sql4 = DB_DataObject::factory(TBL_SMS_CENTER_CUSTOMERS_SERVICES);
$sql5 = DB_DataObject::factory(TBL_SMS_CENTER_ACTIONS);
$sql1->selectAdd();
$sql1->selectAdd($sql1->__table . ".service_id, " .
$sql1->__table . ".service, " .
$sql1->__table . ".watershed, " .
$sql1->__table . ".free_msgs, " .
$sql1->__table . ".type, " .
$sql1->__table . ".multiple_signups, " .
$sql2->__table . ".keyword_id, " .
$sql2->__table . ".keyword, " .
$sql3->__table . ".shortcode_id, " .
$sql3->__table . ".shortcode,
COUNT(DISTINCT " . $sql4->__table . ".customer_services_id) AS total, " .
$sql5->__table . ".action_id");
$sql1->whereAdd($sql1->__table . ".user_id = '" . $_GET['client_id'] . "'");
$sql1->whereAdd($sql1->__table . ".status= 'Active'"); $sql1->whereAdd($sql2->__table . ".status= 'Active'"); $sql1->whereAdd($sql3->__table . ".status= 'Active'"); $sql1->whereAdd($sql4->__table . ".status= 'Active'"); $sql1->whereAdd($sql5->__table . ".status= 'Active'");
$sql2->joinAdd($sql4, 'LEFT');
$sql2->joinAdd($sql3, 'LEFT');
$sql1->joinAdd($sql5, 'LEFT');
$sql1->joinAdd($sql2, 'LEFT');
$sql1->groupBy($sql1->__table . ".service_idASC, " . $sql2->__table . ".keyword_idASC");