简体   繁体   English

使用另一个选择和内部连接选择计数

[英]select count with another select and inner join

Is it possible to use two "select" in the same query?是否可以在同一个查询中使用两个“选择”?

I tried it but got the syntax error several times.我试过了,但多次出现语法错误。

My query example:我的查询示例:

            SELECT 
                comp.id, 
                comp.document, 
                comp.dateStart, 
                comp.companyName, 
                comp.fantasyName, 
                comp.legalNature, 
                comp.mainActivity, 
                comp.situation, 
                comp.shareCapital, 
                comp.idCompanyStatus,
                pp.userCredentialId,
                uc.name,
                cs.name AS 'nameStatus',
                cs.color AS 'colorStatus',
                cs.description,
                comp.idPurchasedProduct, 
                comp.actived, 
                comp.createAt, 
                comp.updateAt,
                comp.phone
            FROM `PurchasedProduct` pp 
            INNER JOIN 
                `Company` comp on comp.idPurchasedProduct = pp.id
            INNER JOIN 
                `UserCustomer` uc on pp.userCredentialId = uc.credentialId
            INNER JOIN
                `CompanyStatus` cs on cs.id = comp.idCompanyStatus
            WHERE 
                comp.actived = 1
            LIMIT 0,5;          
        SELECT COUNT(id) AS totalItems, CEILING(COUNT(id) / 10) AS totalPages FROM Company;

I would like the result shown to be all queries on the screen.我希望结果显示为屏幕上的所有查询。

Basically, what I want is that the result shown when executing the query is the first and second "select" together.基本上,我想要的是执行查询时显示的结果是第一个和第二个“选择”在一起。 I really don't know how or don't understand how to do this.我真的不知道如何或不明白如何做到这一点。

Example:例子:

first result with seconde result第一个结果第二个结果

I want to show both results at once.我想同时显示两个结果。

The documents is fake, not real.文件是假的,不是真的。 Only for demo.仅用于演示。

You should be able to do by having the second query as its own JOIN query.您应该可以通过将第二个查询作为它自己的 JOIN 查询来完成。 Since there is no group by, it is only returning a single row.由于没有 group by,它只返回一行。 By no join condition, the value will be available for every row otherwise.如果没有连接条件,则该值将可用于每一行,否则。 So you SHOULD be able to get by doing所以你应该能够做到

select
      [ all your other columns ],
      JustCounts.TotalItems,
      JustCounts.TotalPages
   from
      [PurchasedProduct and all your other joins]
         JOIN ( SELECT 
                      COUNT(id) AS totalItems, 
                      CEILING(COUNT(id) / 10) AS totalPages 
                   FROM Company ) as JustCounts
   where
      [rest of your original query]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM