[英]Big Mysql Query (Join counts in the result and modify a string)
I am brand new to mysql so please excuse my level of knowledge here, and feel free to direct me in a better direction if what I am doing is out of date. 我是mysql的新手,所以请在这里原谅我的知识水平,如果我做的过时,请随时向我提出更好的指导。
I am pulling information from a database to fill in a php page. 我从数据库中提取信息以填写php页面。
My tables: 我的桌子:
Server: 服务器:
|ServerID (int) | ArticleID (int) | locationID (int) |
| 1 | 46 | 55 |
| 2 | 11 | 81 |
| 3 | 81 | 46 |
| 4 | 55 | 11 |
| 5 | 81 | 99 |
| 5 | 11 | 52 |
Article: 文章:
|ArticleID (int) | Name (varchar) | Typ (int) |
| 46 | domain | 0 |
| 81 | root-server | 1 |
| 55 | vserver | 2 |
| 11 | root-server2 | 1 |
Location: 位置:
|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 |
Result: 结果:
|location (int) | name (varchar) | count (int) |
| 1 | root-server | 1 |
| 1 | root-server2 | 2 |
| 17 | root-server | 1 |
The location in the result is the first number block of the location in the location table (1-5-15-2 -> 1, 1-8-5-8 -> 1, 21-2-5-8 -> 21, 17-2-5-8 -> 17). 结果中的位置是位置表中位置的第一个数字块(1-5-15-2-> 1,1-8-5-8-> 1,21-2-5-8-> 21 ,17-2-5-8-> 17)。 The count is the sum of all servers with the same name and the same first location block.
该计数是具有相同名称和相同第一个位置块的所有服务器的总和。
Do anyone think its possible to get this result in only one query? 有谁认为有可能仅通过一个查询就可以得到此结果?
Thanks for any answer! 感谢您的回答!
Please give this a shot: 请试一下:
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
something like this should work 这样的事情应该工作
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.