Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.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 SQL一次查询两个表并将结果作为JSON对象循环_Mysql_Sql_Vb.net - Fatal编程技术网

Mysql SQL一次查询两个表并将结果作为JSON对象循环

Mysql SQL一次查询两个表并将结果作为JSON对象循环,mysql,sql,vb.net,Mysql,Sql,Vb.net,我不确定我在这里要找什么,所以抱歉,如果这里已经涵盖了这一点,我不确定我需要搜索什么 我有一个MySQL数据库,其中有一个名为Locations的表,看起来有点像这样 id | name | other parameters 1 | shop1 | blah 2 | shop2 | blah {"location":"shop1","queryCount":"1"},{"location":"shop2","queryCount":"3"} 等 和一个客户查询表 id | cus

我不确定我在这里要找什么,所以抱歉,如果这里已经涵盖了这一点,我不确定我需要搜索什么

我有一个MySQL数据库,其中有一个名为Locations的表,看起来有点像这样

id | name | other parameters   
1  | shop1 | blah
2  | shop2 | blah
 {"location":"shop1","queryCount":"1"},{"location":"shop2","queryCount":"3"}

和一个客户查询表

id | customer | department
1  | john     | shop2
2  | Joe      | shop2
3  | James    | shop1
4  | Sue      | shop2

我想查询这个,并返回一个类似这样的JSON对象

id | name | other parameters   
1  | shop1 | blah
2  | shop2 | blah
 {"location":"shop1","queryCount":"1"},{"location":"shop2","queryCount":"3"}
位置表可以随着时间的推移添加到中,显然客户查询也将添加到中,因此两者都需要动态查询

我尝试通过从locations查询中通过一个简单的选择名称获取位置列表,将其转换为一个数组,然后按如下方式循环:

    For i = UBound(listofLocations) To 0 Step -1
        locations.id = listofLocations(i)
        locations.queryCount= RESULT OF: "SELECT COUNT(id) as recordCount from queries WHERE department=listofLocations(i)"
        objectArray.Add(locations)
    Next
这是可行的,但通过循环调用数据库效率很低,如何避免这种情况


谢谢

效率低下是因为您使用的是嵌套查询, 您应该使用LEFT-JOIN,并且只需要一个查询

以下是SQL:-

select l.name, count(*) as recordCount
from Locations as l
left join customer as c
on l.name = c.department
group by l.id;
而且您的模式不是很优化。 您的客户模式应该是

id, customer, location_id <-- using name is redundant, 
                          <-- which should represent in ID (location ID)

首先,我必须建议你们部门换个专业。如果更改“位置”表中名称的拼写,则关系将中断

相反,使用位置表中的id,并设置外键引用

但这是一个旁白。使用您当前的结构,这就是我在SQL中要做的

SELECT
  location.name,
  COUNT(queries.id)        AS count_of_queries
FROM
  locations
LEFT JOIN
  queries
    ON queries.department = locations.name
GROUP BY
  location.name
使用左连接可以确保获得每个位置,即使没有查询

如果查询表中没有关联的记录,则使用COUNTqueries.id而不是COUNT*将得到0

然后可以循环一个查询的结果集,而不是循环多个查询,并构建JSON字符串

可以在SQL中构建字符串,但这通常被认为是不好的做法。更好的做法是让您保持数据的SQL,并让您了解处理和显示数据的php/任何内容。

尝试以下方法:

SELECT T1.name, (SELECT COUNT(*) FROM queries AS T2 WHERE T2.department = T1.name) AS number FROM locations AS T1;

我同意你的方案不合适,你应该通过Id而不是姓名来参考部门,我想你只是在寻找分组

SELECT
    department_id as location,
    COUNT(id) as querycount
FROM
    (table of customer queries)
GROUP BY
    department_id
至于数据库结构。。。如果可能的话,我会稍微改变一下表格:

Location:
id | name | other parameters

Customer:
id | name | whatever

table_of_customer_queries:
id | customer_id | location_id
 1 |      2      |      2
 2 |      4      |      2
 3 |      2      |      1
 4 |      1      |      2
GROUP BY将仅为您提供有查询的部门的结果。如果需要所有部门,可以使用前面提到的左连接选项。

首先,如果位置表确实存储了部门信息,请将其重命名。然后,将Customer.department更改为对Locations.id列的fk引用,并重命名为department_id,而不是名称,这样更不会出错。这可能会也可能不会给你带来性能提升;但是,请记住,数据库中的数据并不是真的要让人可读,而是要让程序可读

在任何一种情况下,查询都可以这样编写:

SELECT a.name, (SELECT COUNT(b.id) 
                FROM Customer as b
                WHERE b.department_id = a.id) as count
FROM Location as a

如果不更改模式,那么答案是使用GROUPBY子句计算每个位置的查询数

创建类似您的表:

create table #locations (id integer, name varchar(20), other text)
create table #queries (id integer, customer varchar(20), department varchar(20))

insert into #locations (id, name, other) values (1, 'shop1', 'blah')
insert into #locations (id, name, other) values (2, 'shop2', 'blah')

insert into #queries (id, customer, department) values (1, 'john', 'shop2')
insert into #queries (id, customer, department) values (2, 'Joe', 'shop2')
insert into #queries (id, customer, department) values (3, 'James', 'shop1')
insert into #queries (id, customer, department) values (4, 'Sue', 'shop2')
查询您的数据:

select
  l.name as location,
  count(q.id) as queryCount
from #locations as l
left join #queries as q on q.department = l.name
group by l.name
order by l.name
结果:

location             queryCount
-------------------- -----------
shop1                1
shop2                3