簡體   English   中英

大Mysql查詢(聯接計入結果並修改字符串)

[英]Big Mysql Query (Join counts in the result and modify a string)

我是mysql的新手,所以請在這里原諒我的知識水平,如果我做的過時,請隨時向我提出更好的指導。

我從數據庫中提取信息以填寫php頁面。

我的桌子:

服務器:

|ServerID (int) | ArticleID (int) | locationID (int) |
|   1           |       46        | 55               |
|   2           |       11        | 81               |
|   3           |       81        | 46               |
|   4           |       55        | 11               |
|   5           |       81        | 99               |
|   5           |       11        | 52               |

文章:

|ArticleID (int)  | Name (varchar)  | Typ (int) |
|   46            |   domain        | 0         |
|   81            |   root-server   | 1         |
|   55            |   vserver       | 2         |
|   11            |   root-server2  | 1         |

位置:

|LocationID (int) | location (varchar) | 
|   46            | 1-5-15-2           |
|   81            | 1-5-14-2           |
|   55            | 2-25-1-9           |
|   11            | 21-2-5-8           |
|   99            | 17-2-5-8           |
|   52            | 1-8-5-8            |

結果:

|location (int) | name (varchar) | count (int) |
| 1             | root-server    | 1           |
| 1             | root-server2   | 2           |
| 17            | root-server    | 1           |

結果中的位置是位置表中位置的第一個數字塊(1-5-15-2-> 1,1-8-5-8-> 1,21-2-5-8-> 21 ,17-2-5-8-> 17)。 該計數是具有相同名稱和相同第一個位置塊的所有服務器的總和。

有誰認為有可能僅通過一個查詢就可以得到此結果?

感謝您的回答!

請試一下:

SELECT 
    s.locationID as id, a.name, count(*) as count
FROM
    `Server` s
    LEFT JOIN
    `Article` a ON s.ArticleID = a.ArticleID
GROUP BY s.locationID, a.name

這樣的事情應該工作

select 
    s.location_id as location, a.name, count(location) as count 
from 
    server as s, article as a 
where 
    s.articleID = a.articleID 
group by location, a.name

檢查一下

SELECT  SUBSTRING_INDEX(location, '-', 1) as LID,Article.Name,Count(*) as Count 
from Location join Server  
on Server.locationID=Location.locationID  
join Article on Article.ArticleID=Server.ArticleID
group by LID,Article.ArticleID  ;

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM