MySQL 的复合查询或者嵌套查询
思韵闪耀
2018-01-25
0
MySQL 的复合查询或者嵌套查询,有表两张,要以 tmpa 表两张为表列,将 tmpb 横向列出,故选择嵌套查询。
create table tmpa  
(  
   tmpaId           bigint not null auto_increment,  
   clrGroupId           bigint,  
   sort                 bigint,  
   name                 varchar(128),  
   primary key (tmpaId)  
); 

create table tmpb  
(  
   tmpbId           bigint not null auto_increment,  
   tmpaId           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 (tmpbId)  
); 

MySQL 复合嵌套查询命令如下

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

查询结果将以 tmpa 为主列,将 tmpb 作为子列,根据条件得到结果。
这里针对 tmpb 的内查询建议加条件,以提高性能。

【版权声明】
本站部分内容来源于互联网,本站不拥有所有权,不承担相关法律责任。如果发现本站有侵权的内容,欢迎发送邮件至masing@13sy.com 举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

相关内容

docker-compos...
1.Compose介绍 DockerCompose是一个用来定义和...
2024-04-26
Mysqldump 时出现...
Mysqldump 时出现错误及解决方案:mysqldump: C...
2024-01-16
MySQL数据库”mysq...
MySQL数据库”mysql SQL Error:1146,SQL...
2023-12-02
mysql关闭binlog...
linux编辑my.cnf,windows编辑my.ini在[my...
2023-12-02
20道Mysql面试题
1. 什么是 MySQL?它与其他数据库管理系统有何不同? 答:M...
2023-11-23
PostgreSQL和My...
PostgreSQL和MySQL对比 下面将从以下几个方面阐述My...
2022-11-03

热门资讯

MySQL权限篇之REPLIC... MySQL权限篇之REPLICATION CLIENT及REPLICATION SLAVE REPL...
configure: erro... php编译后出现这样的信息 以上略 checking for MySQLi support... y...
MySQL清屏命令 在Linux系统下MySQL的清屏命令 MySQL在Linux系统下,输入system clear即...
Skip-External-L... MySQL的配置文件my.cnf中默认存在一行skip-external-locking的参数,即跳...
MySQL 的复合查询或者嵌套... MySQL 的复合查询或者嵌套查询,有表两张,要以 tmpa 表两张为表列,将 tmpb 横向列出,...
MySQL创建用户与授权 一. 创建用户 命令: CREATE USER username@host IDENTIFIED B...
mysql 如何跟踪_MySQ... 在项目开发中,难免会遇到在数据库服务器端跟踪sql执行语句的需求,通过跟踪sql执行语句,我们可以确...
Mysql服务器无法启动 一、Mysql服务器无法启动,错误日志中提示: ^G/usr/local/mysql/bin/mys...
MySql ManifestU... 解决 MySql ManifestUpdate 通过执行计划关闭的方式,不适用超级权限管理人员(公司...
给MySQL的备份账户添加权限 给MySQL的备份账户添加权限 MySQL的备份可以说是重中之重,毕竟数据是一个网站的命脉。 但是备...