MYSQL:子子查询中的主查询值

时间:2014-08-14 11:39:10

标签: mysql civicrm

我目前正在撰写一份非常具体的报告查询。但现在在我的3级子查询中,我似乎无法使用主查询值。当我在那里得到值时,查询将完成

SELECT 
    **`ca`.`id`**, `ca`.`contact_id`,`ca`.`street_address`,`ca`.`postal_code`,`ca`.`city`, 
    IFNULL((
        SELECT 1 
        FROM `civicrm_address` as `casub`
        WHERE `casub`.`master_id` = `ca`.`id`
    ), 0) as `pallet`,
    (
        SELECT SUM(`aantal`) FROM ( 
            SELECT `entity_id`, `bezorggebied_naam_9` as `bezorggebied`,
            (
                SELECT COUNT(*) from `civicrm_address` as `casub`
                LEFT JOIN `civicrm_membership` as `cm` ON `casub`.`contact_id` = `cm`.`contact_id`
                WHERE ( 
                    (SUBSTR(REPLACE(`casub`.`postal_code`, ' ', ''), 1, 4) BETWEEN `bzgarea`.`start_cijfer_range_10` AND `bzgarea`.`eind_cijfer_range_12`)
                        AND
                    (SUBSTR(REPLACE(`casub`.`postal_code`, ' ', ''), -2) BETWEEN `bzgarea`.`start_letter_range_11` AND `bzgarea`.`eind_letter_range_13`)
                )
                AND `cm`.`membership_type_id` in (4,5,6)
                AND `cm`.`status_id` IN (1,2)
                AND (
                     (`cm`.`end_date` IS NULL)
                     OR
                     (`cm`.`end_date` >= now())
                )
            ) as `aantal`
            FROM `civicrm_value_bezorggebieden_6` as `bzgarea`
            WHERE `bzgarea`.`entity_id` IN (
                SELECT contact_id
                FROM `civicrm_address`
                WHERE `id` = **`ca`.`id`** OR `master_id` = **`ca`.`id`**
            )
        ) as `sumTable`
    ) as `tribunes`
FROM `civicrm_address` as `ca`
LEFT JOIN `civicrm_value_bezorggebieden_6` as `cbzg` ON `ca`.`contact_id` = `cbzg`.`entity_id`
WHERE `location_type_id` = 7
AND `master_id` IS NULL
GROUP BY `ca`.`id`
ORDER BY `pallet` DESC

标有**的字段caid假定位于子查询中,但MYSQL返回caid是未知列的错误。

非常感谢帮助!

这里是表格的架构:

-- phpMyAdmin SQL Dump
-- version 3.4.11.1deb2+deb7u1
-- http://www.phpmyadmin.net
--
-- Machine: localhost
-- Genereertijd: 14 aug 2014 om 13:57
-- Serverversie: 5.5.38
-- PHP-Versie: 5.5.15-1~dotdeb.1

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Databank: `civicrm_migratie`
--

DELIMITER $$
--
-- Functies
--
CREATE DEFINER=`civicrm`@`localhost` FUNCTION `civicrm_strip_non_numeric`(input VARCHAR(255) CHARACTER SET utf8) RETURNS varchar(255) CHARSET utf8
    NO SQL
    DETERMINISTIC
BEGIN
      DECLARE output   VARCHAR(255) CHARACTER SET utf8 DEFAULT '';
      DECLARE iterator INT          DEFAULT 1;
      WHILE iterator < (LENGTH(input) + 1) DO
        IF SUBSTRING(input, iterator, 1) IN ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') THEN
          SET output = CONCAT(output, SUBSTRING(input, iterator, 1));
        END IF;
        SET iterator = iterator + 1;
      END WHILE;
      RETURN output;
    END$$

DELIMITER ;

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

--
-- Tabelstructuur voor tabel `civicrm_address`
--

CREATE TABLE IF NOT EXISTS `civicrm_address` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Unique Address ID',
  `contact_id` int(10) unsigned DEFAULT NULL COMMENT 'FK to Contact ID',
  `location_type_id` int(10) unsigned DEFAULT NULL COMMENT 'Which Location does this address belong to.',
  `is_primary` tinyint(4) DEFAULT '0' COMMENT 'Is this the primary address.',
  `is_billing` tinyint(4) DEFAULT '0' COMMENT 'Is this the billing address.',
  `street_address` varchar(96) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Concatenation of all routable street address components (prefix, street number, street name, suffix, unit number OR P.O. Box). Apps should be able to determine physical location with this data (for mapping, mail delivery, etc.).',
  `street_number` int(11) DEFAULT NULL COMMENT 'Numeric portion of address number on the street, e.g. For 112A Main St, the street_number = 112.',
  `street_number_suffix` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Non-numeric portion of address number on the street, e.g. For 112A Main St, the street_number_suffix = A',
  `street_number_predirectional` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Directional prefix, e.g. SE Main St, SE is the prefix.',
  `street_name` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Actual street name, excluding St, Dr, Rd, Ave, e.g. For 112 Main St, the street_name = Main.',
  `street_type` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'St, Rd, Dr, etc.',
  `street_number_postdirectional` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Directional prefix, e.g. Main St S, S is the suffix.',
  `street_unit` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Secondary unit designator, e.g. Apt 3 or Unit # 14, or Bldg 1200',
  `supplemental_address_1` varchar(96) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Supplemental Address Information, Line 1',
  `supplemental_address_2` varchar(96) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Supplemental Address Information, Line 2',
  `supplemental_address_3` varchar(96) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Supplemental Address Information, Line 3',
  `city` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'City, Town or Village Name.',
  `county_id` int(10) unsigned DEFAULT NULL COMMENT 'Which County does this address belong to.',
  `state_province_id` int(10) unsigned DEFAULT NULL COMMENT 'Which State_Province does this address belong to.',
  `postal_code_suffix` varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Store the suffix, like the +4 part in the USPS system.',
  `postal_code` varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Store both US (zip5) AND international postal codes. App is responsible for country/region appropriate validation.',
  `usps_adc` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'USPS Bulk mailing code.',
  `country_id` int(10) unsigned DEFAULT NULL COMMENT 'Which Country does this address belong to.',
  `geo_code_1` double DEFAULT NULL COMMENT 'Latitude',
  `geo_code_2` double DEFAULT NULL COMMENT 'Longitude',
  `manual_geo_code` tinyint(4) DEFAULT '0' COMMENT 'Is this a manually entered geo code',
  `timezone` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Timezone expressed as a UTC offset - e.g. United States CST would be written as "UTC-6".',
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `master_id` int(10) unsigned DEFAULT NULL COMMENT 'FK to Address ID',
  PRIMARY KEY (`id`),
  KEY `index_location_type` (`location_type_id`),
  KEY `index_is_primary` (`is_primary`),
  KEY `index_is_billing` (`is_billing`),
  KEY `index_street_name` (`street_name`),
  KEY `index_city` (`city`),
  KEY `FK_civicrm_address_contact_id` (`contact_id`),
  KEY `FK_civicrm_address_county_id` (`county_id`),
  KEY `FK_civicrm_address_state_province_id` (`state_province_id`),
  KEY `FK_civicrm_address_country_id` (`country_id`),
  KEY `FK_civicrm_address_master_id` (`master_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=146484 ;

--
-- Triggers `civicrm_address`
--
DROP TRIGGER IF EXISTS `civicrm_address_after_insert`;
DELIMITER //
CREATE TRIGGER `civicrm_address_after_insert` AFTER INSERT ON `civicrm_address`
 FOR EACH ROW BEGIN  
UPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.contact_id;
 END
//
DELIMITER ;
DROP TRIGGER IF EXISTS `civicrm_address_after_update`;
DELIMITER //
CREATE TRIGGER `civicrm_address_after_update` AFTER UPDATE ON `civicrm_address`
 FOR EACH ROW BEGIN  
UPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.contact_id;
 END
//
DELIMITER ;
DROP TRIGGER IF EXISTS `civicrm_address_after_delete`;
DELIMITER //
CREATE TRIGGER `civicrm_address_after_delete` AFTER DELETE ON `civicrm_address`
 FOR EACH ROW BEGIN  
UPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = OLD.contact_id;
 END
//
DELIMITER ;

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

--
-- Tabelstructuur voor tabel `civicrm_membership`
--

CREATE TABLE IF NOT EXISTS `civicrm_membership` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Membership Id',
  `contact_id` int(10) unsigned NOT NULL COMMENT 'FK to Contact ID',
  `membership_type_id` int(10) unsigned NOT NULL COMMENT 'FK to Membership Type',
  `join_date` date DEFAULT NULL COMMENT 'Beginning of initial membership period (member since...).',
  `start_date` date DEFAULT NULL COMMENT 'Beginning of current uninterrupted membership period.',
  `end_date` date DEFAULT NULL COMMENT 'Current membership period expire date.',
  `source` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
  `status_id` int(10) unsigned NOT NULL COMMENT 'FK to Membership Status',
  `is_override` tinyint(4) DEFAULT NULL COMMENT 'Admin users may set a manual status which overrides the calculated status. When this flag is true, automated status update scripts should NOT modify status for the record.',
  `owner_membership_id` int(10) unsigned DEFAULT NULL COMMENT 'Optional FK to Parent Membership.',
  `max_related` int(11) DEFAULT NULL COMMENT 'Maximum number of related memberships (membership_type override).',
  `is_test` tinyint(4) DEFAULT '0',
  `is_pay_later` tinyint(4) DEFAULT '0',
  `contribution_recur_id` int(10) unsigned DEFAULT NULL COMMENT 'Conditional foreign key to civicrm_contribution_recur id. Each membership in connection with a recurring contribution carries a foreign key to the recurring contribution record. This assumes we can track these processor initiated events.',
  `campaign_id` int(10) unsigned DEFAULT NULL COMMENT 'The campaign for which this membership is attached.',
  PRIMARY KEY (`id`),
  KEY `index_owner_membership_id` (`owner_membership_id`),
  KEY `FK_civicrm_membership_contact_id` (`contact_id`),
  KEY `FK_civicrm_membership_membership_type_id` (`membership_type_id`),
  KEY `FK_civicrm_membership_status_id` (`status_id`),
  KEY `FK_civicrm_membership_contribution_recur_id` (`contribution_recur_id`),
  KEY `FK_civicrm_membership_campaign_id` (`campaign_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=642452 ;

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

--
-- Tabelstructuur voor tabel `civicrm_value_bezorggebieden_6`
--

CREATE TABLE IF NOT EXISTS `civicrm_value_bezorggebieden_6` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Default MySQL primary key',
  `entity_id` int(10) unsigned NOT NULL COMMENT 'Table that this extends',
  `bezorggebied_naam_9` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `start_cijfer_range_10` int(11) DEFAULT NULL,
  `start_letter_range_11` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
  `eind_cijfer_range_12` int(11) DEFAULT NULL,
  `eind_letter_range_13` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
  `bezorger_14` int(10) unsigned DEFAULT NULL,
  `per_post_42` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_civicrm_value_bezorggebieden_6_entity_id` (`entity_id`),
  KEY `INDEX_bezorggebied_naam_9` (`bezorggebied_naam_9`),
  KEY `INDEX_start_cijfer_range_10` (`start_cijfer_range_10`),
  KEY `INDEX_start_letter_range_11` (`start_letter_range_11`),
  KEY `INDEX_eind_cijfer_range_12` (`eind_cijfer_range_12`),
  KEY `INDEX_eind_letter_range_13` (`eind_letter_range_13`),
  KEY `FK_civicrm_value_bezorggebieden_6_bezorger_14` (`bezorger_14`),
  KEY `INDEX_per_post_42` (`per_post_42`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;

--
-- Beperkingen voor gedumpte tabellen
--

--
-- Beperkingen voor tabel `civicrm_value_bezorggebieden_6`
--
ALTER TABLE `civicrm_value_bezorggebieden_6`
  ADD CONSTRAINT `FK_civicrm_value_bezorggebieden_6_bezorger_14` FOREIGN KEY (`bezorger_14`) REFERENCES `civicrm_contact` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `FK_civicrm_value_bezorggebieden_6_entity_id` FOREIGN KEY (`entity_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE;

0 个答案:

没有答案
相关问题