Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize nested query in SQL

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report