Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
大Mysql查询(结果中的连接计数和修改字符串)_Mysql_Sql_Count - Fatal编程技术网

大Mysql查询(结果中的连接计数和修改字符串)

大Mysql查询(结果中的连接计数和修改字符串),mysql,sql,count,Mysql,Sql,Count,我是mysql的新手,所以请原谅我在这里的知识水平,如果我所做的已经过时,请随时指导我向更好的方向发展 我从数据库中提取信息来填充php页面 我的桌子: 服务器: |ServerID (int) | ArticleID (int) | locationID (int) | | 1 | 46 | 55 | | 2 | 11 | 81 | |

我是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  ;

每个表中是否有更多的项目?看起来您有外键作为主键?什么是类型?为什么结果中没有呢?还有,为什么结果1117中的位置在您的数据库中不存在example@naturalc是的,每个表中都有更多的项目。服务器表有20000项,项目表有500项。女巫类型?结果中的位置是位置表中位置的第一个块。(1-5-15-2 -> 1, 1-8-5-8 -> 1, 21-2-5-8 -> 21, 17-2-5-8 -> 17)