MySQL 的复合查询或者嵌套查询

MySQL 的复合查询或者嵌套查询,有表两张,要以 clrTheme 表两张为表列,将 clrColor 横向列出,故选择嵌套查询。

create table clrTheme
(
   clrThemeId           bigint not null auto_increment,
   clrGroupId           bigint,
   sort                 bigint,
   name                 varchar(128),
   primary key (clrThemeId)
);

create table clrColor
(
   clrColorId           bigint not null auto_increment,
   clrThemeId           bigint,
   sort                 bigint,
   name                 varchar(128),
   alias                varchar(128),
   C                    decimal(8,5),
   M                    decimal(8,5),
   Y                    decimal(8,5),
   K                    decimal(8,5),
   R                    decimal(8,5),
   G                    decimal(8,5),
   B                    decimal(8,5),
   Hex                  varchar(128),
   ColorId              varchar(128),
   primary key (clrColorId)
);

MySQL 复合嵌套查询命令如下

select * from clrTheme as t1,
(
  (select * from clrColor where `sort` = 0) as c1,
  (select * from clrColor where `sort` = 1) as c2,
  (select * from clrColor where `sort` = 2) as c3,
  (select * from clrColor where `sort` = 3) as c4,
  (select * from clrColor where `sort` = 4) as c5
) where
  t1.clrThemeId = c1.clrThemeId
  and t1.clrThemeId = c2.clrThemeId
  and t1.clrThemeId = c3.clrThemeId
  and t1.clrThemeId = c4.clrThemeId
  and t1.clrThemeId = c5.clrThemeId
  order by t1.clrGroupId, t1.sort asc;

查询结果将以 clrTheme 为主列,将 clrColor 作为子列,根据条件得到结果。

这里针对 clrColor 的内查询建议加条件,以提高性能。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值