MYSQL - 由多列Complication组成

时间:2015-02-25 17:11:56

标签: mysql

我在数据库中有这么多记录,我试图按组获取数据,但我的问题是如果现在使用group by one column我正在使用group by name但是其他列没有显示正确的数据

SELECT 
      IF( user_org.active = '0', 
             CONCAT(user_org_profiles.name, ' (disabled)' ),
             user_org_profiles.name ) AS name, 
      user_org.oid AS id, 
      IF( 149 =149 AND user_org_profiles_ext.account != '', 
             user_org_profiles_ext.account, 
             user_org.oid ) AS dealerid 
   FROM 
      user_org 
         LEFT JOIN user_org_profiles 
            ON user_org.oid = user_org_profiles.oid 
         LEFT JOIN user_org_profiles_ext 
            ON user_org.oid = user_org_profiles.oid 
   WHERE 
          user_org.active = '1' 
      AND user_org_profiles.name != '' 
   GROUP BY 
      name

我得到了这个输出

enter image description here

因为我只使用按名称分组,以便dealerid的相同但我需要像这样的输出 enter image description here

这里有一些实际数据

CREATE TABLE IF NOT EXISTS `user_org` (
`oid` int(11) NOT NULL AUTO_INCREMENT,
`active` tinyint(1) NOT NULL,
`suffix` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`orgkey` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
`gid` int(11) NOT NULL,
 PRIMARY KEY (`oid`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=902 ;
INSERT INTO `user_org` (`oid`, `active`, `suffix`, `orgkey`, `gid`) VALUES
(1, 0, 'wire1', 'Aahsoas_-_', 3),
(2, 1, 'int-ohs2', 'Interiors_with_', 3),
(3, 1, 'artah3', 'Ar_Sp_-_U', 3),
(4, 0, ' 4', 'Aqc_Pls_&', 3),
(5, 1, 'goc5', 'Arc_pa_&_Le', 3),
(6, 1, 'copes6', 'Coo_Pc', 3),
(7, 1, 'cms', 'CMSol', 3),
(8, 0, 'hotl8', 'Clool', 3),
(9, 0, 'tet9', 'Coome_', 3),
(10, 0, ' 10', 'Tore', 3),
(11, 0, 'kco11', 'Nie_I', 3),
(12, 0, ' 12', 'Hil_In', 3),
(14, 0, ' 14', 'Brough_En', 3),
(15, 0, 'Letbs15', 'Leisme_Ho', 3),
(16, 0, 'telt16', 'Whit_me', 3);

第二桌

CREATE TABLE IF NOT EXISTS `user_org_profiles` (
`oid` int(11) NOT NULL,
`name` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
 PRIMARY KEY (`oid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `user_org_profiles` (`oid`, `name`) VALUES
(1, 'Aaas - ko'),
(2, 'Int and ahs'),
(3, 'Art- ah'),
(4, 'Aqls & as'),
(5, 'Art pure'),
(6, 'Colo Ps'),
(7, 'CMS Cons'),
(8, 'Clearas'),
(9, 'Colare'),
(10, 'Thre'),
(11, 'Ninc.'),
(12, 'Hiltd.'),
(14, 'Breatd.'),
(15, 'Leisbs'),
(16, 'Whitre');

第3桌

CREATE TABLE IF NOT EXISTS `user_org_profiles_ext` (
`oid` int(11) NOT NULL,
`account` varchar(12) NOT NULL,
 PRIMARY KEY (`oid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `user_org_profiles_ext` (`oid`, `account`) VALUES
(1,'C-0006'),
(2,'C-0056'),
(3,'C-0030'),
(4,' '),
(5,'C-0016'),
(6,'C-0246'),
(7,'C-0050'),
(8,'C-0037'),
(9,'COL001'),
(10,' '),
(11,'C-0157'),
(12,'HIL002'),
(14,' '),
(15,'LEI01U'),
(16,' ');

1 个答案:

答案 0 :(得分:0)

尝试更改为"分组1"其中1表示列表中的序数列与数据的实际NAME列。

或只是通过

粘贴整个第一列
group by
   IF( user_org.active = '0', 
             CONCAT(user_org_profiles.name, ' (disabled)' ),
             user_org_profiles.name )

您的user_org_profiles有一个左连接,但是将AND添加到您的where子句,将其转换为INNER JOIN。此外,您的IF(149 = 149 AND ...将始终将第一部分视为有效且可以被排除,只留下条件的其他部分。

最后,既然你是盟友没有做任何聚合,我会忽略GROUP BY而只是改为不同

SELECT DISTINCT
      IF( user_org.active = '0', 
             CONCAT(user_org_profiles.name, ' (disabled)' ),
             user_org_profiles.name ) AS name, 
      user_org.oid AS id, 
      IF(    user_org_profiles_ext.account = '', 
             user_org.oid, user_org_profiles_ext.account ) AS dealerid 
   FROM 
      user_org 
         JOIN user_org_profiles 
            ON user_org.oid = user_org_profiles.oid 
         LEFT JOIN user_org_profiles_ext 
            ON user_org.oid = user_org_profiles_ext.oid 
   WHERE 
          user_org.active = '1'
      AND user_org_profiles.name != '' 

最终结果......

每个示例数据并实际尝试查询,结果是由于JOIN子句不正确。第二个实例是加入USER_ORG_PROFILES而不是USER_ORG_PROFILES_EXT。