Skip to content
Advertisement

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.

Advertisement

Answer

You should be able to do by having the second query as its own JOIN query. Since there is no group by, it is only returning a single row. 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]
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement