[英]Combine two mysql queries with different where clauses
I have results across multiple locations. 我在多个位置都有结果。 The locations are stored in one table and the results in another. 位置存储在一个表中,结果存储在另一个表中。 I query every result with a location in the netherlands. 我用荷兰的位置查询每个结果。 I group those results by the province their in. After wich i use the lattitude, longitude to mark the results per province at google maps. 我将这些结果按省份分组。在使用纬度,经度后,我会在Google地图上标记每个省份的结果。
The query: 查询:
SELECT loc.location_id as loc_id, loc.lat, loc.long, count(res.unique_id) as aantal, loc.land, loc.provincie, loc.gemeente, loc.plaats
FROM `vj_results_1618048029` AS res, vj_location AS loc
WHERE res.time > '1324635105' AND res.time < '1327313506' AND res.search_id='8070' AND (loc.lat != '0' AND loc.long != '0') AND res.location_id != '' AND res.location_id = loc.location_id AND loc.land = 'Nederland'
GROUP BY loc.provincie
ORDER BY loc.land, loc.provincie, loc.gemeente, loc.plaats DESC
The result: 结果:
loc_id lat long aantal land provincie gemeente plaats
19 52.132633 5.291266 5732 Nederland
672 52.926438 6.791355 670 Nederland Drenthe Borger-Odoorn Borger
67 52.518537 5.471422 597 Nederland Flevoland Lelystad Lelystad
401 53.1641642 5.7817542 812 Nederland Friesland
52 51.842867 5.854622 3486 Nederland Gelderland Nijmegen Nijmegen
63 53.2193835 6.5665018 890 Nederland Groningen Groningen Groningen
185 50.9555426 5.8248384 670 Nederland Limburg Sittard-Geleen Geleen
183 51.5657089 5.081203 3241 Nederland Noord-Brabant Tilburg Tilburg
168 52.632281 4.750806 6044 Nederland Noord-Holland Alkmaar Alkmaar
223 52.3116551 6.9268283 1324 Nederland Overijssel Oldenzaal Oldenzaal
37 52.0901422 5.1096649 2291 Nederland Utrecht Utrecht Utrecht
2723 51.5332946 4.2162383 413 Nederland Zeeland Tholen Tholen
65 51.924216 4.481776 5842 Nederland Zuid-Holland Rotterdam Rotterdam
The problem is that the lattitude longitude i recieve are those of the narrowest location of the first result in the group. 问题是我收到的纬度经度是组中第一个结果的最窄位置的经度。 For flevoland it is the city 'lelystad' and for zeeland the town 'tolen'. 对于弗莱福兰来说,它是城市“莱利斯塔德”,对于泽兰德来说是“城市”。 In 'friesland' its accurate because the first result did not specify a town, just the province. 在“弗里斯兰省”之所以准确无误,是因为第一个结果未指定城镇,仅指定了省份。
To get the right lattitude and longitude i would query something like this: 为了获得正确的纬度和经度,我会查询如下内容:
SELECT location_id as loc_id, loc.lat, loc.long, loc.land, loc.provincie, loc.gemeente, loc.plaats
FROM vj_location AS loc
WHERE (loc.lat != '0' AND loc.long != '0') AND loc.land = 'Nederland' AND loc.gemeente = '' AND loc. plaats = ''
GROUP BY loc.provincie
ORDER BY loc.land, loc.provincie, loc.gemeente, loc.plaats DESC
Wich would give me these results: 威奇会给我这些结果:
loc_id lat long land provincie gemeente plaats
19 52.132633 5.291266 Nederland
697 52.9476012 6.6230586 Nederland Drenthe
1257 52.5279781 5.5953508 Nederland Flevoland
401 53.1641642 5.7817542 Nederland Friesland
235 52.045155 5.8718234 Nederland Gelderland
2732 53.2887213 6.7060867 Nederland Groningen
860 51.4427238 6.0608726 Nederland Limburg
255 51.4826537 5.2321687 Nederland Noord-Brabant
407 52.5205869 4.788474 Nederland Noord-Holland
884 52.4387814 6.5016411 Nederland Overijssel
3796 52.119508 5.1943653 Nederland Utrecht
3692 51.325362 3.6640464 Nederland Zeeland
705 52.0207975 4.4937836 Nederland Zuid-Holland
I need to combine those to a single query, with the results and the group from the first query and the lattitude and longitude from the second. 我需要将这些合并到一个查询中,并使用第一个查询的结果和组,以及第二个查询的纬度和经度。 Could anybody assist? 有人可以协助吗?
Thanks in advance 提前致谢
edit: I tried it using a UNION but it returns a error : 1222 - The used SELECT statements have a different number of columns 编辑:我尝试使用UNION,但它返回错误:1222-使用的SELECT语句具有不同的列数
I tried using subqueries but it slows the query down.. It now takes ages to run a query 我尝试使用子查询,但它降低了查询速度。现在运行查询需要花费很多时间
2nd edit: Thanks to @johntotetwoo i edited the union query to make it work. 第2次修改:感谢@johntotetwoo,我编辑了联合查询以使其正常运行。 I had to remove the order by because my mysql did not like that, did not need it really was for the readability while debugging. 我必须删除该命令,因为我的mysql不喜欢这样做,因为调试时的可读性并不需要它。 I also changed the order of the select in the second query so that the zero's where put at the right column. 我还更改了第二个查询中select的顺序,以便将零放在右边的列中。
The new query 新查询
SELECT *
FROM
(SELECT loc.location_id as loc_id, loc.lat, loc.long, count(res.unique_id) as aantal, loc.land, loc.provincie, loc.gemeente, loc.plaats
FROM `vj_results_1618048029` AS res, vj_location AS loc
WHERE res.time > '1324635105' AND res.time < '1327313506' AND res.search_id='8070' AND (loc.lat != '0' AND loc.long != '0') AND res.location_id != '' AND res.location_id = loc.location_id AND loc.land = 'Nederland'
GROUP BY loc.provincie
UNION
SELECT location_id as loc_id, loc.lat, loc.long, 0 as aantal, loc.land, loc.provincie, loc.gemeente, loc.plaats
FROM vj_location AS loc
WHERE (loc.lat != '0' AND loc.long != '0') AND loc.land = 'Nederland' AND loc.gemeente = '' AND loc. plaats = ''
GROUP BY loc.provincie) as iTable
It works great now, thanks! 现在效果很好,谢谢! I just wondered if it could be combined to 1 row (i now have a row with the nr. of results and the province, and another row with the same province and the right longitude and lattitude.) If those could be combined it would be completely perfect! 我只是想知道是否可以将其合并为1行(我现在有一个结果和省份的行,而另一行具有相同的省份以及正确的经度和纬度。)如果可以合并,那将是完全完美!
将字段aantal添加到第二个查询并重新结合
it it because the second query doesn't have aantal
column. 之所以这样,是因为第二个查询没有aantal
列。
try this: 尝试这个:
SELECT *
FROM
(SELECT loc.location_id as loc_id, loc.lat, loc.long, count(res.unique_id) as aantal, loc.land, loc.provincie, loc.gemeente, loc.plaats
FROM `vj_results_1618048029` AS res, vj_location AS loc
WHERE res.time > '1324635105' AND res.time < '1327313506' AND res.search_id='8070' AND (loc.lat != '0' AND loc.long != '0') AND res.location_id != '' AND res.location_id = loc.location_id AND loc.land = 'Nederland'
GROUP BY loc.provincie
ORDER BY loc.land, loc.provincie, loc.gemeente, loc.plaats DESC
UNION
SELECT location_id as loc_id, loc.lat, loc.long, loc.land, loc.provincie, loc.gemeente, loc.plaats , 0 as aantal
FROM vj_location AS loc
WHERE (loc.lat != '0' AND loc.long != '0') AND loc.land = 'Nederland' AND loc.gemeente = '' AND loc. plaats = ''
GROUP BY loc.provincie
ORDER BY loc.land, loc.provincie, loc.gemeente, loc.plaats DESC) as iTable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.