2017-06-15 110 views
0

這裏是我的表:我如何計算總體結果並將它們分組?

// posts 
+----+-----------+--------------------+------------+--------+ 
| id | title |  body  | author_id | amount | 
+----+-----------+--------------------+------------+--------+ 
| 1 | post1  | somthing   | 2543  | 5000 | 
| 2 | post2  | something else  | 4352  | NULL | 
| 3 | post3  | whatever   | 1563  | 1200 | 
| 4 | post4  | test context  | 7234  | NULL | 
| 5 | post5  | anything ...  | 4352  | NULL | 
+----+-----------+--------------------+------------+--------+ 

另外我有兩個疑問:

SELECT COUNT(*), COUNT(amount) FROM posts 
+----------+---------------+ 
| COUNT(*) | COUNT(amount) | 
+----------+---------------+ 
| 5  | 2    | 
+----------+---------------+ 

SELECT * FROM posts ORDER BY id LIMIT 0,2 
+----+-----------+--------------------+------------+--------+ 
| id | title |  body  | author_id | amount | 
+----+-----------+--------------------+------------+--------+ 
| 1 | post1  | somthing   | 2543  | 5000 | 
| 2 | post2  | something else  | 4352  | NULL | 
+----+-----------+--------------------+------------+--------+ 

現在,我想這兩個查詢組合,這是預期的結果:

+----+-----------+--------------------+------------+--------+----------+---------------+ 
| id | title |  body  | author_id | amount | COUNT(*) | COUNT(amount) | 
+----+-----------+--------------------+------------+--------+----------+---------------+ 
| 1 | post1  | somthing   | 2543  | 5000 | 5  | 2    | 
| 2 | post2  | something else  | 4352  | NULL | 5  | 2    | 
+----+-----------+--------------------+------------+--------+----------+---------------+ 

我該如何做到這一點?這是我迄今爲止嘗試過的錯誤,它總是返回一行:

SELECT x.*, COUNT(*), COUNT(amount) 
FROM (
    SELECT * FROM posts ORDER BY id 
) x 
LIMIT 0,2 
+0

將第二個查詢交叉連接到第一個。 – xQbert

回答

2

如果我明白你的想法是正確的。交叉連接查詢shoudl這樣的伎倆..

SELECT * 
FROM posts p 
CROSS JOIN (SELECT COUNT(*) cnt, COUNT(amount) cntamt FROM posts) t 
ORDER BY p.id 
LIMIT 0,2 

ALTERNATE也許(未經測試)......但是同樣有一個子查詢......但只有1 where子句。

SELECT id, title, body, author_id, max(cnt) cnt, max(cntAmount) cntAmount 
FROM (SELECT id 
      , title 
      , body 
      , author_id 
      , @[email protected]+1 as cnt 
      , case when Amount is not null then @cntamt:[email protected]+1 else @cntAmt end as cntAmount 
     FROM posts p 
     CROSS JOIN (SELECT @cnt:=0,@cntAmt:=0) t 
     GROUP BY id, title, body, author_id) Z 
ORDER BY z.id 
LIMIT 0,2 
+0

那麼[現實](https://stackoverflow.com/questions/44566376/how-can-i-use-sub-query-to-count-the-result-before-limiting)這兩個查詢有一個相同的複雜'where'子句,我試圖提高性能並匹配一次結果。但就你而言,你仍然選擇兩次。 –

+0

如果mySQL支持窗口/分析函數,那麼這可以在1個查詢中實現。但是,它沒有。 – xQbert

+0

謝謝。你爲什麼在'select'語句中使用所有列的名字?爲什麼不只是'*'? –

2

你可以用CROSS JOIN來做到這一點。像這樣:

SELECT 
    posts.*, 
    tbl.nbr, 
    tbl.nbrAmount 
FROM 
    posts 
CROSS JOIN 
    (SELECT COUNT(*) as nbr, COUNT(amount) as nbrAmount FROM posts) AS tbl 
ORDER BY id LIMIT 0,2 
+0

那麼[現實](https://stackoverflow.com/questions/44566376/how-can-i-use-sub-query-to-count-the-result-before-limiting)這兩個查詢有一個相同的複雜'where'子句,我試圖提高性能並匹配一次結果。但就你而言,你仍然選擇兩次。 –

0

使用加入你可以結合這一點。

SELECT p1.*,p2.* FROM posts AS p1 
LEFT JOIN (
    SELECT COUNT(*), COUNT(amount) FROM posts 
) AS p2 ON 1=1 
ORDER BY p1.id LIMIT 0,2 
相關問題