2个SQL查询一次

时间:2015-12-09 14:11:44

标签: mysql sql

如何连接这两个查询,以便我的结果中可以有3列:postcode,memberCount和placeCount

SELECT LEFT(`delivery_postcode`, 2) as `postcode`, count(`delivery_postcode`) as `count`
FROM `customer_cards`
WHERE `delivery_postcode` IS NOT NULL
AND `delivery_postcode` <> ''
GROUP BY `postcode`
ORDER BY `count` DESC

SELECT LEFT(`placePostcode`, 2) as `postcode`, count(`placePostcode`) as `placeCount`
FROM `RestaurantsForGoogleMaps`
WHERE `placePostcode` IS NOT NULL
AND `placePostcode` <> ''
GROUP BY `postcode`
ORDER BY `placeCount` DESC

目前,对于任一查询

,我的结果如下所示
postcode | count/placeCount
------------------------
SW       | 817
W1       | 533
EC       | 395

4 个答案:

答案 0 :(得分:1)

    SELECT postcode,count,placecount FROM (SELECT LEFT(`delivery_postcode`, 2) as `postcode`, count(`delivery_postcode`) as `count`, 0 as `placecount`
    FROM `customer_cards`
    WHERE `delivery_postcode` IS NOT NULL
    AND `delivery_postcode` <> ''
    GROUP BY `postcode`
    UNION
SELECT LEFT(`placePostcode`, 2) as `postcode`, count(`placePostcode`) as `placecount`,0 as `count`
    FROM `RestaurantsForGoogleMaps`
    WHERE `placePostcode` IS NOT NULL
    AND `placePostcode` <> ''
    GROUP BY `postcode` )  ORDER BY count desc

答案 1 :(得分:1)

这样的事情应该有效:

SELECT LEFT(`delivery_postcode`, 2) as `postcode`
,     count(`delivery_postcode`) as `count`
,     pc.placeCount
FROM `customer_cards` cc
LEFT JOIN  (
               SELECT LEFT(`placePostcode`, 2) as `postcode`, 
               count(`placePostcode`) as `placeCount`
               FROM `RestaurantsForGoogleMaps`
               WHERE `placePostcode` IS NOT NULL
                   AND `placePostcode` <> ''
              GROUP BY `postcode`
           ) pc
       on  pc.postcode = LEFT(cc.delivery_postcode, 2)
WHERE `delivery_postcode` IS NOT NULL
AND `delivery_postcode` <> ''
GROUP BY `postcode`
ORDER BY `count` DESC

答案 2 :(得分:0)

将两个查询创建为内联视图,然后加入邮政编码。这假设两个查询都返回所需的结果,并且POST CODE是要加入的键,并且没有其他属性需要正确完成连接。它还假设邮政编码将在两个表中。

如果没有,那么我们必须左右复制连接并将结果合并在一起。

SELECT A.postcode, A.mcount, B.placecount
FROM
  (SELECT LEFT(delivery_postcode, 2) as postcode, count(delivery_postcode) as mcount
  FROM customer_cards
  WHERE delivery_postcode IS NOT NULL
  AND delivery_postcode <> ''
  GROUP BY LEFT(delivery_postcode, 2)) A
INNER JOIN (
  SELECT LEFT(placePostcode, 2) as postcode, count(placePostcode) as placeCount
  FROM RestaurantsForGoogleMaps
  WHERE placePostcode IS NOT NULL
  AND placePostcode <> ''
  GROUP BY LEFT(placePostcode, 2)) B
on A.postcode = B.postcode
order by A.postcode

答案 3 :(得分:0)

目前,对于任一查询

,我的结果如下所示
;WITH PC1 AS (
SELECT LEFT(`delivery_postcode`, 2) as `postcode`, count(`delivery_postcode`) as `count`
FROM `customer_cards`
WHERE `delivery_postcode` IS NOT NULL
AND `delivery_postcode` <> ''
GROUP BY `postcode`
ORDER BY `count` DESC

),
PC2 AS (

SELECT LEFT(`placePostcode`, 2) as `postcode`, count(`placePostcode`) as `placeCount`
FROM `RestaurantsForGoogleMaps`
WHERE `placePostcode` IS NOT NULL
AND `placePostcode` <> ''
GROUP BY `postcode`
ORDER BY `placeCount` DESC

)

SELECT a.postcode, a.count,b.placeCount
FROM PC1 a
LEFT JOIN PC2 b
ON a.postcode = b.postcode