Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize nested query in SQL

Shulou Source: shulou.com Published: 2022-05-31 20:48:45 10月24日 Update

SQL how to achieve nested queries, I believe that many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

A good system integrates good programs and good database design.

To do that, you need experience.

This is a nested query I wrote in conjunction with UINON.

The five aspects of the report into a temporary table, and then from the temporary table, the required data query out.

The copy code is as follows:

$sql="SELECT type , sum( yjsl ) as yjsl , sum( yysl ) as yysl, sum( jyrs ) as jyrs, sum( jycs ) as jycs

FROM (

SELECT c.mc AS

TYPE , count( d.lsh ) AS yjsl, 0 AS yysl, 0 AS jyrs, 0 AS jycs

FROM sys_dzxxb AS b, sys_jcb AS c, sys_dzyjb AS d

WHERE b.bm = c.lsh

AND d.dzlsh = b.lsh

GROUP BY c.mc

UNION SELECT c.mc AS

TYPE , 0 AS yjsl, count( e.lsh ) AS yysl, 0 AS jyrs, 0 AS jycs

FROM sys_dzxxb AS b, sys_jcb AS c, sys_dzyy AS e

WHERE b.bm = c.lsh

AND e.dzlsh = b.lsh

GROUP BY c.mc

UNION SELECT c.mc AS

TYPE , 0 AS yjsl, 0 AS yysl, count( DISTINCT e.dzlsh ) AS jyrs, 0 AS jycs

FROM sys_dzxxb AS b, sys_jcb AS c, sys_ltxxb AS e

WHERE b.bm = c.lsh

AND e.dzlsh = b.lsh

GROUP BY c.mc

UNION SELECT c.mc AS

TYPE , 0 AS yjsl, 0 AS yysl, 0 AS jyrs, count( DISTINCT e.lsh ) AS jycs

FROM sys_dzxxb AS b, sys_jcb AS c, sys_ltxxb AS e

WHERE b.bm = c.lsh

AND e.dzlsh = b.lsh

GROUP BY c.mc

) AS temptable

GROUP BY TYPE ";

After reading the above, do you know how to implement nested queries in SQL? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Tags: Queries excellence code content data methods more experience table problems helpless for this reason skills reports databases data queries aspects programs Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker OPPO Reno Shulou Technology Shulou Information